Skip to content

Commit

Permalink
fix: only use optionalReplacementSpan if client supports InsertReplace (
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Sep 12, 2022
1 parent 0960a6b commit 899ba6b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function asCompletionItem(entry: tsp.CompletionEntry, optionalReplacement
}

let insertText = entry.insertText;
const replacementSpan = entry.replacementSpan || optionalReplacementSpan;
const replacementSpan = entry.replacementSpan || (features.completionInsertReplaceSupport ? optionalReplacementSpan : undefined);
let replacementRange = replacementSpan && Range.fromTextSpan(replacementSpan);
// Make sure we only replace a single line at most
if (replacementRange && replacementRange.start.line !== replacementRange.end.line) {
Expand Down
37 changes: 26 additions & 11 deletions src/lsp-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('completion', () => {
server.didCloseTextDocument({ textDocument: doc });
});

it('completions for clients that do not support insertReplaceSupport', async () => {
it('completions for clients that support insertReplaceSupport', async () => {
const doc = {
uri: uri('bar.ts'),
languageId: 'typescript',
Expand All @@ -221,16 +221,16 @@ describe('completion', () => {
const completion = proposals!.items.find(completion => completion.label === 'getById');
assert.isDefined(completion);
assert.isDefined(completion!.textEdit);
assert.containsAllKeys(completion!.textEdit, ['newText', 'range']);
assert.containsAllKeys(completion!.textEdit, ['newText', 'insert', 'replace']);
server.didCloseTextDocument({ textDocument: doc });
});

it('completions for clients that support insertReplaceSupport', async () => {
it('completions for clients that do not support insertReplaceSupport', async () => {
const clientCapabilitiesOverride: lsp.ClientCapabilities = {
textDocument: {
completion: {
completionItem: {
insertReplaceSupport: true,
insertReplaceSupport: false,
},
},
},
Expand Down Expand Up @@ -258,8 +258,7 @@ describe('completion', () => {
assert.isNotNull(proposals);
const completion = proposals!.items.find(completion => completion.label === 'getById');
assert.isDefined(completion);
assert.isDefined(completion!.textEdit);
assert.containsAllKeys(completion!.textEdit, ['newText', 'insert', 'replace']);
assert.isUndefined(completion!.textEdit);
localServer.didCloseTextDocument({ textDocument: doc });
localServer.closeAll();
localServer.shutdown();
Expand Down Expand Up @@ -401,7 +400,7 @@ describe('completion', () => {
return true;
}
test("fs/")
test("fs/r")
`,
};
server.didOpenTextDocument({ textDocument: doc });
Expand All @@ -417,11 +416,27 @@ describe('completion', () => {
const completion = proposals!.items.find(completion => completion.label === 'fs/read');
assert.strictEqual(completion!.label, 'fs/read');
assert.deepStrictEqual(completion!.textEdit, {
range: {
start: { line: 5, character: 20 },
end: { line: 5, character: 23 },
},
newText: 'fs/read',
insert: {
start: {
line: 5,
character: 20,
},
end: {
line: 5,
character: 23,
},
},
replace: {
start: {
line: 5,
character: 20,
},
end: {
line: 5,
character: 24,
},
},
});
});

Expand Down
1 change: 1 addition & 0 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const DEFAULT_TEST_CLIENT_CAPABILITIES: lsp.ClientCapabilities = {
textDocument: {
completion: {
completionItem: {
insertReplaceSupport: true,
snippetSupport: true,
labelDetailsSupport: true,
},
Expand Down

0 comments on commit 899ba6b

Please sign in to comment.