public SignatureHelp GetSignatureHelp(TextDocument document, Position position) { string text = document.Text; int index = document.GetPosition(position); string[] lines = text.Split('\n'); string line = lines[position.Line]; int startIndex = line.Substring(0, position.Character).LastIndexOf("(", StringComparison.Ordinal); if (startIndex != -1) { string functionName = line.Substring(0, startIndex).Trim().Split(" ").Last(); // Remove the current line because it might contain errors. lines[position.Line] = ""; DustContext currentContext = document.GetContextAtPosition(position, Project.CompileFile(string.Join('\n', lines)).GlobalContext); Function function = currentContext.GetFunction(functionName); StringBuilder labelBuilder = new StringBuilder($"{function.Name}("); if (function.Parameters.Length > 0) { for (int i = 0; i < function.Parameters.Length; i++) { labelBuilder.Append($"{function.Parameters[i].Identifier.Name}: any"); if (i != function.Parameters.Length - 1) { labelBuilder.Append(", "); } } } labelBuilder.Append("): any"); return(new SignatureHelp { ActiveParameter = line.Substring(startIndex, line.IndexOf(")", startIndex, StringComparison.Ordinal) - startIndex).Count(character => character == ','), ActiveSignature = 0, Signatures = new[] { new SignatureInformation { Label = labelBuilder.ToString(), Parameters = function.Parameters.Select(parameter => new ParameterInformation { Label = parameter.Identifier.Name + ": any" }).ToArray() } } }); } return(new SignatureHelp()); }
public Hover GetHover(TextDocument document, Position position) { string word = document.GetWordAtPosition(position); DustContext context = document.GetContextAtPosition(position, Project.CompileFile(document.Text).GlobalContext); string content = ""; if (context.ContainsPropety(word)) { content = context.GetProperty(word).GetDetail(); } if (context.ContainsFunction(word)) { content = context.GetFunction(word).GetDetail(); } if (string.IsNullOrEmpty(content) == false) { return(new Hover { Contents = (StringOrObject <MarkedString>) $@"```dust {content} ```" }); } return(new Hover()); /* * * string name = document.GetWordAtPosition(position); * string[] lines = document.Text.Split('\n'); * * lines[position.Line] = ""; * * DustContext context = document.GetContextAtPosition(position, Project.CompileFile(string.Join('\n', lines))); * * Function function = context.GetFunction(name); * * if (function != null) * { * return new Hover * { * Contents = new StringOrObject<MarkedString>(new MarkedString * { * Value = function.GetDetail(), * Language = "dust" * }) * }; * } * * return new Hover(); */ }
public List <CompletionItem> GetCompletions(TextDocument document, Position position) { string[] lines = document.Text.Split('\n'); string word = lines[position.Line].Substring(0, position.Character).Trim().Split(" ").Last(); List <CompletionItem> completions = new List <CompletionItem>(); bool found = false; foreach (KeyValuePair <string[], CompletionItem[]> completion in keywordCompletions) { foreach (string entry in completion.Key) { if (word == entry) { found = true; completions.AddRange(completion.Value); break; } } } if (found == false) { // Remove the current line because it might contain errors. lines[position.Line] = ""; DustContext globalContext = Project.CompileFile(string.Join('\n', lines)).GlobalContext; DustContext currentContext = document.GetContextAtPosition(position, globalContext); currentContext.Functions.Union(globalContext.Functions).DistinctBy(function => function.Name).ToList().ForEach(function => completions.Add(new CompletionItem { Label = function.Name, Kind = CompletionItemKind.Function, Detail = function.GetDetail() })); currentContext.Properties.Union(globalContext.Properties).ToList().ForEach(property => completions.Add(new CompletionItem { Label = property.Name, Kind = CompletionItemKind.Variable, Detail = property.GetDetail() })); completions.AddRange(new[] { new CompletionItem { Label = "let", Kind = CompletionItemKind.Keyword }, new CompletionItem { Label = "public", Kind = CompletionItemKind.Keyword }, new CompletionItem { Label = "internal", Kind = CompletionItemKind.Keyword }, new CompletionItem { Label = "private", Kind = CompletionItemKind.Keyword } }); } return(completions); }