Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong import completion when insert/replace supported #592

Merged
merged 3 commits into from
Sep 26, 2022

Conversation

rchl
Copy link
Member

@rchl rchl commented Sep 25, 2022

Fixes regression for clients that have insertReplaceSupport where completing:

import { readFil }

would result in incorrect insert:

import { readFile } from "fs"; }

The TextEdit in this case would be:

      "textEdit": {
        "insert": {
          "end": {
            "character": 16,
            "line": 0
          },
          "start": {
            "character": 0,
            "line": 0
          }
        },
        "newText": "import { readFile$1 } from \"fs\";",
        "replace": {
          "end": {
            "character": 18,
            "line": 0
          },
          "start": {
            "character": 0,
            "line": 0
          }
        }
      }

and the problem would be in the insert range.

We should not be providing both insert and replace ranges when TS completion entry has its own replacementSpan provided as in that case it's the only valid option.

@predragnikolic

@rchl rchl changed the title fix: wrong import completion with insert/replace support fix: wrong import completion when insert/replace supported Sep 25, 2022
if (isSnippet && !features.completionSnippets) {
return null;
}
if (features.completionSnippets && (isSnippet || entry.isImportStatementCompletion || item.kind === lsp.CompletionItemKind.Function || item.kind === lsp.CompletionItemKind.Method)) {
if (features.completionSnippets && (isSnippet || item.kind === lsp.CompletionItemKind.Function || item.kind === lsp.CompletionItemKind.Method)) {
Copy link
Member Author

@rchl rchl Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entry.isImportStatementCompletion check was removed as it's only provided by TS for telemetry reasons and isSnippet check covers that case already.

return item;
}

function getRangeFromReplacementSpan(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is pretty similar to what VS Code does:

    // vscode/extensions/typescript-language-features/src/languageFeatures/completions.ts
	private getRangeFromReplacementSpan(tsEntry: Proto.CompletionEntry, completionContext: CompletionContext) {
		if (!tsEntry.replacementSpan) {
			return;
		}

		let replaceRange = typeConverters.Range.fromTextSpan(tsEntry.replacementSpan);
		// Make sure we only replace a single line at most
		if (!replaceRange.isSingleLine) {
			replaceRange = new vscode.Range(replaceRange.start.line, replaceRange.start.character, replaceRange.start.line, completionContext.line.length);
		}

		// If TS returns an explicit replacement range, we should use it for both types of completion
		return {
			inserting: replaceRange,
			replacing: replaceRange,
		};
	}

So basically if an entry has a replacementSpan, VS Code uses that replacementSpan for both insert and replace.

You took a little different approach, but it works.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VSCode doesn't use LSP so we can't use exactly same code but yes, it's based on VSCode's logic.

Copy link
Contributor

@predragnikolic predragnikolic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the regression ✅

@rchl rchl merged commit 4fe902a into master Sep 26, 2022
@rchl rchl deleted the fix/import-completion branch September 26, 2022 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants