Changes, fixes and features for autocompletion

This commit is contained in:
Deukhoofd 2018-11-29 20:46:30 +01:00
parent 1760e72117
commit c7b8ef3463
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 70 additions and 68 deletions

View File

@ -50,16 +50,6 @@ namespace UpsilonLanguageServer
using (var script = Executor.ParseInput(content, Options))
{
session.SourceText = script.ScriptString;
if (script.Diagnostics.Errors.Count > 0)
{
foreach (var error in script.Diagnostics.Errors)
{
diag.Add(ConvertToVsCodeDiagnostic(error));
}
return;
}
var bound = script.Bind();
foreach (var error in script.Diagnostics.Errors)
{
@ -70,10 +60,7 @@ namespace UpsilonLanguageServer
diag.Add(ConvertToVsCodeDiagnostic(warning));
}
if (script.Diagnostics.Errors.Count == 0)
{
session.Bound = bound;
}
session.Bound = bound;
}
}
catch (Exception e)

View File

@ -30,7 +30,7 @@ namespace UpsilonLanguageServer.Services
{
HoverProvider = true,
SignatureHelpProvider = new SignatureHelpOptions("()"),
CompletionProvider = new CompletionOptions(true, "."),
CompletionProvider = new CompletionOptions(true, ""),
TextDocumentSync = new TextDocumentSyncOptions
{
OpenClose = true,

View File

@ -10,6 +10,7 @@ using LanguageServer.VsCode.Contracts;
using Upsilon.Binder;
using Upsilon.BoundTypes;
using Upsilon.Utilities;
using Type = Upsilon.BaseTypes.Type;
namespace UpsilonLanguageServer.Services
{
@ -47,6 +48,10 @@ namespace UpsilonLanguageServer.Services
contents.Append($"\n\nReturns: {fVar.ResultType}");
}
}
else if (findNode is BoundFunctionExpression fe)
{
contents.Append($"\n\nReturns: {fe.ReturnType}");
}
return new Hover()
{
@ -128,71 +133,53 @@ namespace UpsilonLanguageServer.Services
{
if (Session.Documents.TryGetValue(textDocument.Uri, out var doc) && doc.Bound != null)
{
if (context.TriggerCharacter == '.')
var linePos = doc.SourceText.GetLineStartPos(position.Line);
var characterPosition = linePos + position.Character;
using (var nodeIterator = doc.Bound.GetNodeAtPosition(characterPosition).GetEnumerator())
{
var linePos = doc.SourceText.GetLineStartPos(position.Line);
var findNode = doc.Bound.GetBottomNodeAtPosition(linePos + position.Character - 2);
if (findNode is BoundVariableSymbol variableSymbol)
if (nodeIterator.MoveNext())
{
var result = await GetListFromVariableSymbol(variableSymbol);
if (result != null)
var node = nodeIterator.Current;
if (node.Kind != BoundKind.BoundFullstopIndexExpression && nodeIterator.MoveNext())
{
return result;
node = nodeIterator.Current;
}
if (node is BoundFullStopIndexExpression indexExpression)
{
var expression = indexExpression.Expression;
var variableSymbol = ResolveVariable(expression);
var result = await GetListFromVariableSymbol(variableSymbol);
if (result != null)
{
return result;
}
}
}
}
else
{
var linePos = doc.SourceText.GetLineStartPos(position.Line);
var characterPosition = linePos + position.Character;
using (var nodeIterator = doc.Bound.GetNodeAtPosition(characterPosition).GetEnumerator())
{
if (nodeIterator.MoveNext())
{
var node = nodeIterator.Current;
if (node.Kind != BoundKind.BoundFullstopIndexExpression && nodeIterator.MoveNext())
{
node = nodeIterator.Current;
}
if (node is BoundFullStopIndexExpression indexExpression)
{
var expression = indexExpression.Expression;
if (expression.Kind == BoundKind.VariableExpression)
{
var variableExpression = (BoundVariableExpression)expression;
var result = await GetListFromVariableSymbol(variableExpression.Variable);
if (result != null)
{
return result;
}
}
}
}
}
var scope = doc.Bound.GetScopeAt(characterPosition);
var variables = scope.GetBoundScopeVisibleVariables();
if (scope != null)
{
return new CompletionList(
variables.Select(x =>
{
var (key, value) = x;
return new CompletionItem(key,
CompletionItemKind.Variable, value.Type.ToString(),
x.Value.CommentValue == null
? ""
: $"\n\n{string.Join(" \n", value.CommentValue)}");
}));
}
var scope = doc.Bound.GetScopeAt(characterPosition);
var variables = scope.GetBoundScopeVisibleVariables();
if (scope != null)
{
return new CompletionList(
variables.Select(x =>
{
var (key, value) = x;
return new CompletionItem(key,
CompletionItemKind.Variable, value.Type.ToString(),
x.Value.CommentValue == null
? ""
: $"\n\n{string.Join(" \n", value.CommentValue)}");
}));
}
}
return new CompletionList(new CompletionItem[0], false);
}
private static async Task<CompletionList> GetListFromVariableSymbol(BoundVariableSymbol variableSymbol)
private static async Task<CompletionList> GetListFromVariableSymbol(VariableSymbol variableSymbol)
{
if (variableSymbol.VariableSymbol is FunctionParameterSymbol parameterSymbol &&
if (variableSymbol is FunctionParameterSymbol parameterSymbol &&
parameterSymbol.BoundTypeDefinition is UserDataBoundTypeDefinition udBoundDef)
{
return new CompletionList(
@ -203,7 +190,7 @@ namespace UpsilonLanguageServer.Services
$"{value.ActualType}({value.Type})", $"{value.Comment}", null);
}));
}
if (variableSymbol.VariableSymbol is TableVariableSymbol tableSymbol)
if (variableSymbol is TableVariableSymbol tableSymbol)
{
return new CompletionList(
tableSymbol.Variables.Select(x =>
@ -220,5 +207,33 @@ namespace UpsilonLanguageServer.Services
return null;
}
private VariableSymbol ResolveVariable(BoundExpression expression)
{
if (expression.Kind == BoundKind.VariableExpression)
{
var variableExpression = (BoundVariableExpression) expression;
return variableExpression.Variable.VariableSymbol;
}
if (expression.Kind == BoundKind.BoundFullstopIndexExpression)
{
var fullStopIndexExpression = (BoundFullStopIndexExpression) expression;
var indexerExpression = fullStopIndexExpression.Expression;
var indexerVariable = ResolveVariable(indexerExpression);
if (indexerVariable.Type == Type.Table)
{
return ((TableVariableSymbol)indexerVariable).Variables[fullStopIndexExpression.Index];
}
if (indexerVariable.Type == Type.UserData)
{
var bDefProperty = ((UserDataBoundTypeDefinition) ((FunctionParameterSymbol) indexerVariable)
.BoundTypeDefinition).Properties[fullStopIndexExpression.Index];
var boundDef = BoundTypeHandler.GetTypeDefinition(bDefProperty.ActualType);
return new FunctionParameterSymbol(fullStopIndexExpression.Index, boundDef);
}
}
return null;
}
}
}