Skip to content

Commit

Permalink
Fix completion when being at a '-' of a kebab identifier. (#2087)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Feb 15, 2024
1 parent cf59fdf commit 8ef843a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
29 changes: 19 additions & 10 deletions src/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -983,14 +983,15 @@ ast::Unit* LocationLanguageServerPipeline::parse(Source* source) {
const uint8* text = source->text();
int offset = compute_source_offset(text, line_number_, column_number_);

// We only provide completions after a `-` if we are after a " --".
if (offset >= 1 && text[offset - 1] == '-') {
if (offset < 3 ||
text[offset - 1] != '-' ||
text[offset - 2] != '-' ||
text[offset - 3] != ' ') {
exit(0);
}
// We only provide completions after a '-' if there isn't a space in
// front of the '-', and if we don't have 'foo--'. That is, a '--'
// without a space in front.
if (offset >= 2 && text[offset - 1] == '-' &&
(text[offset - 2] == ' ' || text[offset - 2] == '\n')) {
exit(0);
}
if (offset >= 3 && text[offset - 1] == '-' && text[offset - 2] == '-' && text[offset - 3] != ' ') {
exit(0);
}

LspSource lsp_source(source, offset);
Expand All @@ -1014,8 +1015,16 @@ Source* CompletionPipeline::_load_file(const char* path, const PackageLock& pack
int start_offset = offset;
IdentifierValidator validator;
validator.disable_start_check();
while (start_offset > 0 &&
validator.check_next_char(text[start_offset - 1], [&]() { return text[start_offset]; })) {
while (true) {
if (start_offset <= 0) break;
auto peek = [&]() {
if (offset == start_offset) return LSP_SELECTION_MARKER;
return text[start_offset];
};
// Walk backwards as long as it's a valid identifier character.
if (!validator.check_next_char(text[start_offset - 1], peek)) {
break;
}
start_offset--;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const int TAB_WIDTH = 8;
// specific location acts as a marker or is just an illegal character.
// If the callback returns false, then it's illegal. Otherwise the callback should
// replace the marker with the original character and return true.
const int LSP_SELECTION_MARKER = 1;
const uint8 LSP_SELECTION_MARKER = 1;

class LspSource : public Source {
public:
Expand Down
12 changes: 12 additions & 0 deletions tests/lsp/basic-completion-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ main:
+ call
- member, ==, true, null, false
*/

toplevel-fun 499
/* ^~~~~~~~
+ toplevel-fun
- main, global, SomeClass, null, true, false
*/

toplevel-fun 499
/* ^~~~~~~
+ toplevel-fun
- main, global, SomeClass, null, true, false
*/

0 comments on commit 8ef843a

Please sign in to comment.