Skip to content

Commit

Permalink
Repl fixes (#462)
Browse files Browse the repository at this point in the history
* Update ReplPromptCallbacks.cs

* Update ReplSessionTests.cs

* Update SemanticModel.cs
  • Loading branch information
LPeter1997 authored Sep 1, 2024
1 parent ff864c8 commit 3bc7edd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/Draco.Compiler.Tests/Scripting/ReplSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public void BasicExpressions(string input, object? output)
}

[InlineData("func add(x: int32, y: int32) = x + y;")]
[InlineData("List()")]
[InlineData("System.Collections.Generic.List()")]
[Theory]
public void InvalidEntries(string input)
{
Expand Down
1 change: 1 addition & 0 deletions src/Draco.Compiler/Api/Semantics/SemanticModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private ImmutableArray<Diagnostic> GetDiagnostics(SourceSpan? span = null)
{
case CompilationUnitSyntax:
case FunctionDeclarationSyntax:
case ScriptEntrySyntax:
{
containingSymbol?.Bind(this);
break;
Expand Down
28 changes: 14 additions & 14 deletions src/Draco.Repl/ReplPromptCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,13 @@ protected override Task<IReadOnlyList<CompletionItem>> GetCompletionItemsAsync(
var semanticModel = script.Compilation.GetSemanticModel(tree);

var cursorPosition = tree.IndexToSyntaxPosition(caret);
var tokenAtCaret = tree.Root
.TraverseSubtreesAtCursorPosition(cursorPosition)
.OfType<SyntaxToken>()
.LastOrDefault();

var completionItems = this.completionService.GetCompletions(semanticModel, caret);

var result = new List<CompletionItem>();
foreach (var item in completionItems)
{
var replacementText = item.Edits[0].Text;

if (tokenAtCaret is not null && tokenAtCaret.Text.Length > 0)
{
// We can filter by prefix here
if (!replacementText.StartsWith(tokenAtCaret.Text, StringComparison.OrdinalIgnoreCase))
{
continue;
}
}

var coloring = CompletionKindToSyntaxColoring(item.Kind);
var format = configuration.SyntaxColors.Get(coloring);
var completion = new CompletionItem(
Expand All @@ -97,6 +83,20 @@ protected override Task<IReadOnlyList<CompletionItem>> GetCompletionItemsAsync(
return Task.FromResult<IReadOnlyList<CompletionItem>>(result);
}

protected override Task<bool> ShouldOpenCompletionWindowAsync(
string text, int caret, KeyPress keyPress, CancellationToken cancellationToken)
{
var script = this.MakeScript(text);
var tree = script.Compilation.SyntaxTrees.Single();
var semanticModel = script.Compilation.GetSemanticModel(tree);

var cursorPosition = tree.IndexToSyntaxPosition(caret);

var completionItems = this.completionService.GetCompletions(semanticModel, caret);

return Task.FromResult(completionItems.Any());
}

private Script<object?> MakeScript(string text) => Script.Create(
code: text,
globalImports: session.GlobalImports,
Expand Down

0 comments on commit 3bc7edd

Please sign in to comment.