diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-03-11 18:26:59 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-03-11 18:26:59 +0200 |
| commit | ac251de510cfb75c2967f85efc17ac8f8ad20ea0 (patch) | |
| tree | a1806f6494ac27176e40ad3c4953a60ba8ca0e96 /Software/Visual_Studio/TEMP | |
| parent | b0bbc0bec1aaed2230b8a4aa1c08e379fed4629a (diff) | |
| download | Tango-ac251de510cfb75c2967f85efc17ac8f8ad20ea0.tar.gz Tango-ac251de510cfb75c2967f85efc17ac8f8ad20ea0.zip | |
Working on script engine...
Diffstat (limited to 'Software/Visual_Studio/TEMP')
4 files changed, 168 insertions, 43 deletions
diff --git a/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting.Editors/ScriptEditor.cs b/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting.Editors/ScriptEditor.cs index bcc05bde5..d2f5dd169 100644 --- a/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting.Editors/ScriptEditor.cs +++ b/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting.Editors/ScriptEditor.cs @@ -558,6 +558,8 @@ namespace Tango.Scripting.Editors InvalidateHighlighting(); } + _declaredTypes = declaredTypes; + //for (int i = 0; i < TextArea.TextView.LineTransformers.Count; i++) //{ // if (TextArea.TextView.LineTransformers[i] is OffsetColorizer) @@ -680,10 +682,12 @@ namespace Tango.Scripting.Editors HidePopup(); var lineText = GetCurrentLineText(); - var previousWordsLast = GetPreviousWords().LastOrDefault(); + var previousWords = GetPreviousWords(); + var previousWordsLast = previousWords.LastOrDefault(); String currentWord = previousWordsLast != null ? previousWordsLast.Replace("\t", "") : String.Empty; String currentWordIncludingParenthesis = currentWord.Split('(').LastOrDefault(); + if (previousWords.Count > 0 && previousWords.First().StartsWith("//")) return; if (e.Text == " " && GetPreviousWord() == "new") { @@ -758,7 +762,51 @@ namespace Tango.Scripting.Editors if (declaredType != null) { + completionWindow.HideCompletion(); + IList<ICompletionData> data = new List<ICompletionData>(); + + var typeMembers = declaredType.Symbols.ToList(); + + foreach (var methodGroup in typeMembers.GroupBy(x => x.Name)) + { + var member = methodGroup.First(); + if (member.Kind == SymbolKind.Method) + { + data.Add(new MethodCompletionItem() + { + Class = declaredType.Name, + Name = member.Name, + ReturnType = member.Type, + Description = member.Summary, + //Parameters = method.Parameters, + Overloads = methodGroup.Count() - 1, + }); + + } + else if (member.Kind == SymbolKind.Property) + { + data.Add(new PropertyCompletionItem() + { + Class = declaredType.Name, + Name = member.Name, + Type = member.Type, + Description = member.Summary, + }); + } + else if (member.Kind == SymbolKind.Field) + { + data.Add(new FieldCompletionItem() + { + Class = declaredType.Name, + Name = member.Name, + Type = member.Type, + Description = member.Summary, + }); + } + } + + ShowCompletionWindow(data, GetCurrentWord()); } } } @@ -766,29 +814,34 @@ namespace Tango.Scripting.Editors { completionWindow.HideCompletion(); - var session = GetConstructionSession(); - - if (session != null) + try { - var content = CreateConstructionSessionPopupContent(session); - if (content.Methods.Count > 0) + var session = GetConstructionSession(); + + if (session != null) { - ShowPopup(content); + var content = CreateConstructionSessionPopupContent(session); + if (content.Methods.Count > 0) + { + ShowPopup(content); + } } - } - var methodSession = GetMethodSession(); + var methodSession = GetMethodSession(); - if (methodSession != null) - { - var content = CreateMethodSessionPopupContent(methodSession); - if (content.Methods.Count > 0) + if (methodSession != null) { - ShowPopup(content); + var content = CreateMethodSessionPopupContent(methodSession); + if (content.Methods.Count > 0) + { + ShowPopup(content); + } } } - - var a = GetMethodSession(); + catch (Exception ex) + { + Debug.WriteLine(ex); + } } else if (lineText.StartsWith("using")) { @@ -851,7 +904,7 @@ namespace Tango.Scripting.Editors data.Add(new StructCompletionItem() { Name = type.Name, - Description = "Declared inside the current script...", + Description = type.Summary, Namespace = type.ContainingNamespace, Priority = 1, }); @@ -861,7 +914,7 @@ namespace Tango.Scripting.Editors data.Add(new EnumCompletionItem() { Name = type.Name, - Description = "Declared inside the current script...", + Description = type.Summary, Namespace = type.ContainingNamespace, Priority = 1, }); @@ -871,7 +924,7 @@ namespace Tango.Scripting.Editors data.Add(new InterfaceCompletionItem() { Name = type.Name, - Description = "Declared inside the current script...", + Description = type.Summary, Namespace = type.ContainingNamespace, Priority = 1, }); @@ -881,7 +934,7 @@ namespace Tango.Scripting.Editors data.Add(new ClassCompletionItem() { Name = type.Name, - Description = "Declared inside the current script...", + Description = type.Summary, Namespace = type.ContainingNamespace, Priority = 1, }); @@ -940,36 +993,25 @@ namespace Tango.Scripting.Editors } } - foreach (var symbol in _parser.GetContextSymbols(Document.Text, CaretOffset)) + foreach (var symbol in _parser.GetContextSymbols(Document.Text, CaretOffset).Where(x => x.Name.StartsWith(GetCurrentWord()))) { if (symbol.Kind == SymbolKind.Property) { data.Add(new PropertyCompletionItem() { Class = symbol.Class, - Description = "Property defined in this script.", - Name = symbol.Name, - Type = symbol.Type, - Priority = 2, - }); - } - else if (symbol.Kind == SymbolKind.Field || symbol.Kind == SymbolKind.Local) - { - data.Add(new FieldCompletionItem() - { - Class = symbol.Class, - Description = "Field defined in this script.", + Description = symbol.Summary, Name = symbol.Name, Type = symbol.Type, Priority = 2, }); } - else if (symbol.Kind == SymbolKind.Parameter) + else if (symbol.Kind == SymbolKind.Field || symbol.Kind == SymbolKind.Local || symbol.Kind == SymbolKind.Parameter) { data.Add(new FieldCompletionItem() { Class = symbol.Class, - Description = "Method parameter defined in this script.", + Description = symbol.Summary, Name = symbol.Name, Type = symbol.Type, Priority = 2, @@ -980,7 +1022,7 @@ namespace Tango.Scripting.Editors data.Add(new MethodCompletionItem() { Class = symbol.Class, - Description = "Method defined in this script.", + Description = symbol.Summary, Name = symbol.Name, ReturnType = symbol.Type, Priority = 2, diff --git a/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptParser.cs b/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptParser.cs index 1ee2b780c..b0c18ce83 100644 --- a/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptParser.cs +++ b/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptParser.cs @@ -13,6 +13,19 @@ namespace Tango.Scripting.Parsing { public class ScriptParser { + private const string fakeScript = "CurrentScript"; + private string fakeScriptName = "CurrentScript."; + + private String ReplaceFakeScript(String str) + { + if (str != null) + { + return str.Replace(fakeScriptName, "").Replace(fakeScript, ""); + } + + return str; + } + private CompilationUnitSyntax GetRoot(String code) { SyntaxTree tree = CSharpSyntaxTree.ParseText(code); @@ -31,6 +44,24 @@ namespace Tango.Scripting.Parsing public List<ScriptSymbol> GetContextSymbols(String code, int caretOffset) { + var currentNode = GetCaretOffsetNode(code, caretOffset); + + if (currentNode.Ancestors().OfType<BaseTypeDeclarationSyntax>().Count() == 0) + { + var usings = GetUsingsFull(code); + + int removedLength = usings.Select(x => x.Length).Sum(); + + foreach (var use in usings) + { + code = code.Replace(use, ""); + } + + String scriptStart = $"public class {fakeScript}\n{{\n"; + code = $"{scriptStart}{code}\n}}"; + caretOffset = caretOffset - removedLength + scriptStart.Length; + } + var model = GetSemanticModel(code); var symbols = model.LookupSymbols(caretOffset).ToList().Where(x => x.Kind == SymbolKind.Property || x.Kind == SymbolKind.Field || x.Kind == SymbolKind.Parameter || x.Kind == SymbolKind.Local || x.Kind == SymbolKind.Method).ToList(); @@ -47,11 +78,12 @@ namespace Tango.Scripting.Parsing vars.Add(new ScriptSymbol() { Name = symbol.Name, - Type = prop.GetValue(symbol).ToString(), - Class = symbol.ContainingType?.Name, + Type = ReplaceFakeScript(prop.GetValue(symbol).ToString()), + Class = ReplaceFakeScript(symbol.ContainingType?.Name), Kind = symbol.Kind, Accessibility = symbol.DeclaredAccessibility, - ContainingNamespace = symbol.ContainingNamespace?.Name, + ContainingNamespace = ReplaceFakeScript(symbol.ContainingNamespace?.Name), + Summary = GetSymbolDocumentation(symbol), }); } } @@ -64,11 +96,12 @@ namespace Tango.Scripting.Parsing vars.Add(new ScriptSymbol() { Name = symbol.Name, - Type = prop.GetValue(symbol).ToString(), - Class = symbol.ContainingType?.Name, + Type = ReplaceFakeScript(prop.GetValue(symbol).ToString()), + Class = ReplaceFakeScript(symbol.ContainingType?.Name), Kind = symbol.Kind, Accessibility = symbol.DeclaredAccessibility, - ContainingNamespace = symbol.ContainingNamespace?.Name, + ContainingNamespace = ReplaceFakeScript(symbol.ContainingNamespace?.Name), + Summary = GetSymbolDocumentation(symbol), }); } } @@ -84,6 +117,13 @@ namespace Tango.Scripting.Parsing return matches.OfType<Match>().Select(x => x.Value.Replace("using ", "").Replace(";", "").Replace("\n", "").Replace("\t", "").Replace("\r", "")).ToList(); } + public List<String> GetUsingsFull(String code) + { + Regex r = new Regex("(using [^;^\n]+;)"); + var matches = r.Matches(code); + return matches.OfType<Match>().Select(x => x.Value).ToList(); + } + public List<ScriptType> GetDeclaredTypes(String code) { List<ScriptType> types = new List<ScriptType>(); @@ -102,6 +142,8 @@ namespace Tango.Scripting.Parsing scriptType.Name = type.Name; scriptType.Kind = type.TypeKind; scriptType.ContainingNamespace = type.ContainingNamespace?.Name; + scriptType.Summary = GetNodeDocumentation(d); + foreach (var symbol in d.DescendantNodes().OfType<PropertyDeclarationSyntax>()) { @@ -115,12 +157,13 @@ namespace Tango.Scripting.Parsing Name = symbolModel.Name, Type = symbolModel.Type.ToString(), ContainingNamespace = symbolModel.ContainingNamespace?.Name, + Summary = GetNodeDocumentation(symbol), }); } foreach (var symbol in d.DescendantNodes().OfType<FieldDeclarationSyntax>()) { - var symbolModel = model.GetDeclaredSymbol(symbol.Declaration.Variables.FirstOrDefault()) as IFieldSymbol; + var symbolModel = model.GetDeclaredSymbol(symbol.Declaration.Variables.FirstOrDefault()) as IFieldSymbol; if (symbolModel != null) { @@ -132,6 +175,7 @@ namespace Tango.Scripting.Parsing Name = symbolModel.Name, Type = symbolModel.Type.ToString(), ContainingNamespace = symbolModel.ContainingNamespace?.Name, + Summary = GetNodeDocumentation(symbol), }); } } @@ -150,6 +194,7 @@ namespace Tango.Scripting.Parsing Name = symbolModel.Name, Type = symbolModel.ReturnType.ToString(), ContainingNamespace = symbolModel.ContainingNamespace?.Name, + Summary = GetNodeDocumentation(symbol), }); } } @@ -198,5 +243,41 @@ namespace Tango.Scripting.Parsing { return node.Ancestors().ToList(); } + + private String GetNodeDocumentation(SyntaxNode node) + { + try + { + var trivia = node.GetLeadingTrivia().FirstOrDefault(t => t.Kind() == SyntaxKind.SingleLineCommentTrivia); + + if (trivia != null && !String.IsNullOrWhiteSpace(trivia.ToString())) + { + return trivia.ToString().Replace("//", ""); + } + } + catch { } + + return "No documentation."; + } + + private String GetSymbolDocumentation(ISymbol symbol) + { + if (symbol != null) + { + var prop = symbol.GetType().GetProperty("SyntaxNode"); + + if (prop != null) + { + var node = prop.GetValue(symbol) as SyntaxNode; + + if (node != null) + { + return GetNodeDocumentation(node.Parent); + } + } + } + + return "No documentation."; + } } } diff --git a/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptSymbol.cs b/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptSymbol.cs index b93b7951a..622ca4406 100644 --- a/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptSymbol.cs +++ b/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptSymbol.cs @@ -15,6 +15,7 @@ namespace Tango.Scripting.Parsing public String Class { get; set; } public Accessibility Accessibility { get; set; } public String ContainingNamespace { get; set; } + public String Summary { get; set; } public override string ToString() { diff --git a/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptType.cs b/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptType.cs index fc360acb5..3ca34a85e 100644 --- a/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptType.cs +++ b/Software/Visual_Studio/TEMP/Tango.Scripting/Tango.Scripting/Parsing/ScriptType.cs @@ -13,6 +13,7 @@ namespace Tango.Scripting.Parsing public TypeKind Kind { get; set; } public List<ScriptSymbol> Symbols { get; set; } public string ContainingNamespace { get; set; } + public String Summary { get; set; } public ScriptType() { |
