Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 3ed3f6c

Browse files
committed
Improve implementation
1 parent 5cca7fa commit 3ed3f6c

File tree

3 files changed

+179
-163
lines changed

3 files changed

+179
-163
lines changed

src/lang-handler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export interface LanguageClient {
6464
* Can occur as as a result of rename or executeCommand (code action).
6565
* @param params The edits to apply to the workspace
6666
*/
67-
workspaceApplyEdit(params: ApplyWorkspaceEditParams): Promise<ApplyWorkspaceEditResponse>;
67+
workspaceApplyEdit(params: ApplyWorkspaceEditParams, childOf?: Span): Promise<ApplyWorkspaceEditResponse>;
6868
}
6969

7070
/**
@@ -197,11 +197,12 @@ export class RemoteLanguageClient {
197197
}
198198

199199
/**
200-
* Requests a set of text changes to be applied to documents in the workspace
201-
* Can occur as as a result of rename or executeCommand (code action).
202-
* @param params The edits to apply to the workspace
200+
* The workspace/applyEdit request is sent from the server to the client to modify resource on
201+
* the client side.
202+
*
203+
* @param params The edits to apply.
203204
*/
204-
workspaceApplyEdit(params: ApplyWorkspaceEditParams): Promise<ApplyWorkspaceEditResponse> {
205-
return this.request('workspace/applyEdit', params).toPromise();
205+
workspaceApplyEdit(params: ApplyWorkspaceEditParams, childOf = new Span()): Promise<ApplyWorkspaceEditResponse> {
206+
return this.request('workspace/applyEdit', params, childOf).toPromise();
206207
}
207208
}

src/test/typescript-service-helpers.ts

Lines changed: 60 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as chai from 'chai';
22
import * as sinon from 'sinon';
33
import * as ts from 'typescript';
44
import { CompletionItemKind, CompletionList, DiagnosticSeverity, TextDocumentIdentifier, TextDocumentItem, WorkspaceEdit } from 'vscode-languageserver';
5-
import { Hover, Location, SignatureHelp, SymbolInformation, SymbolKind } from 'vscode-languageserver-types';
5+
import { Command, Diagnostic, Hover, Location, SignatureHelp, SymbolInformation, SymbolKind } from 'vscode-languageserver-types';
66
import { LanguageClient, RemoteLanguageClient } from '../lang-handler';
77
import { TextDocumentContentParams, WorkspaceFilesParams } from '../request-type';
88
import { SymbolLocationInformation } from '../request-type';
@@ -2311,40 +2311,40 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor
23112311
});
23122312
});
23132313

2314-
describe('textDocumentCodeAction()', function (this: TestContext) {
2314+
describe('textDocumentCodeAction()', function (this: TestContext & ISuiteCallbackContext) {
23152315
beforeEach(initializeTypeScriptService(createService, rootUri, new Map([
2316-
[rootUri + 'package.json', '{ "name": "mypkg" }'],
2316+
[rootUri + 'package.json', JSON.stringify({ name: 'mypkg' })],
23172317
[rootUri + 'a.ts', [
23182318
'class A {',
2319-
' constructor() {',
2320-
' missingThis = 33;',
2321-
' }',
2319+
'\tconstructor() {',
2320+
'\t\tmissingThis = 33;',
2321+
'\t}',
23222322
'}',
23232323
'const a = new A();'
23242324
].join('\n')]
23252325
])) as any);
23262326

23272327
afterEach(shutdownService as any);
23282328

2329-
it('suggests a missing this', async function (this: TestContext) {
2329+
it('suggests a missing this', async function (this: TestContext & ITestCallbackContext) {
23302330
await this.service.textDocumentDidOpen({
23312331
textDocument: {
23322332
uri: rootUri + 'a.ts',
23332333
languageId: 'typescript',
23342334
text: [
23352335
'class A {',
2336-
' missingThis: number;',
2337-
' constructor() {',
2338-
' missingThis = 33;',
2339-
' }',
2336+
'\tmissingThis: number;',
2337+
'\tconstructor() {',
2338+
'\t\tmissingThis = 33;',
2339+
'\t}',
23402340
'}',
23412341
'const a = new A();'
23422342
].join('\n'),
23432343
version: 1
23442344
}
23452345
});
23462346

2347-
const firstDiagnostic = {
2347+
const firstDiagnostic: Diagnostic = {
23482348
range: {
23492349
start: { line: 3, character: 4 },
23502350
end: { line: 3, character: 15 }
@@ -2354,7 +2354,7 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor
23542354
code: 2663,
23552355
source: 'ts'
23562356
};
2357-
const actions = await this.service.textDocumentCodeAction({
2357+
const actions: Command[] = await this.service.textDocumentCodeAction({
23582358
textDocument: {
23592359
uri: rootUri + 'a.ts'
23602360
},
@@ -2363,31 +2363,24 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor
23632363
diagnostics: [firstDiagnostic]
23642364
}
23652365
}).toArray().map(patches => apply(null, patches)).toPromise();
2366-
assert.lengthOf(actions, 1);
2367-
assert.sameDeepMembers(actions, [
2368-
{
2369-
title: 'Add \'this.\' to unresolved variable.',
2370-
command: 'codeFix',
2371-
arguments: [
2372-
{
2373-
fileName: uri2path(rootUri + 'a.ts'),
2374-
textChanges: [
2375-
{
2376-
span: { start: 50, length: 15 },
2377-
newText: '\t this.missingThis'
2378-
}
2379-
]
2380-
}
2381-
]
2382-
}
2383-
]);
2366+
assert.deepEqual(actions, [{
2367+
title: 'Add \'this.\' to unresolved variable.',
2368+
command: 'codeFix',
2369+
arguments: [{
2370+
fileName: uri2path(rootUri + 'a.ts'),
2371+
textChanges: [{
2372+
span: { start: 49, length: 13 },
2373+
newText: '\t\tthis.missingThis'
2374+
}]
2375+
}]
2376+
}]);
23842377

2385-
} as any);
2386-
} as any);
2378+
});
2379+
});
23872380

2388-
describe('workspaceExecuteCommand()', function (this: TestContext) {
2381+
describe('workspaceExecuteCommand()', function (this: TestContext & ISuiteCallbackContext) {
23892382
beforeEach(initializeTypeScriptService(createService, rootUri, new Map([
2390-
[rootUri + 'package.json', '{ "name": "mypkg" }'],
2383+
[rootUri + 'package.json', JSON.stringify({ name: 'mypkg' })],
23912384
[rootUri + 'a.ts', [
23922385
'class A {',
23932386
' constructor() {',
@@ -2400,47 +2393,43 @@ export function describeTypeScriptService(createService: TypeScriptServiceFactor
24002393

24012394
afterEach(shutdownService as any);
24022395

2403-
it('should return edits for a codeFix command', async function (this: TestContext) {
2404-
const result = await this.service.workspaceExecuteCommand({
2405-
command: 'codeFix',
2406-
arguments: [
2407-
{
2396+
describe('codeFix', () => {
2397+
it('should apply a WorkspaceEdit for the passed FileTextChanges', async function (this: TestContext & ITestCallbackContext) {
2398+
await this.service.workspaceExecuteCommand({
2399+
command: 'codeFix',
2400+
arguments: [{
24082401
fileName: uri2path(rootUri + 'a.ts'),
2409-
textChanges: [
2410-
{
2411-
span: { start: 50, length: 15 },
2412-
newText: '\t this.missingThis'
2413-
}
2414-
]
2415-
}
2416-
]
2417-
}).toArray().map(patches => apply(null, patches)).toPromise();
2418-
2419-
assert.isUndefined(result);
2402+
textChanges: [{
2403+
span: { start: 50, length: 15 },
2404+
newText: '\t\tthis.missingThis'
2405+
}]
2406+
}]
2407+
}).toArray().map(patches => apply(null, patches)).toPromise();
24202408

2421-
sinon.assert.called(this.client.workspaceApplyEdit);
2422-
const workspaceEdit = this.client.workspaceApplyEdit.lastCall.args[0];
2423-
assert.deepEqual(workspaceEdit, {
2424-
edit: {
2425-
changes: {
2426-
[rootUri + 'a.ts']: [{
2427-
newText: '\t this.missingThis',
2428-
range: {
2429-
end: {
2430-
character: 9,
2431-
line: 5
2432-
},
2433-
start: {
2434-
character: 0,
2435-
line: 3
2409+
sinon.assert.calledOnce(this.client.workspaceApplyEdit);
2410+
const workspaceEdit = this.client.workspaceApplyEdit.lastCall.args[0];
2411+
assert.deepEqual(workspaceEdit, {
2412+
edit: {
2413+
changes: {
2414+
[rootUri + 'a.ts']: [{
2415+
newText: '\t\tthis.missingThis',
2416+
range: {
2417+
end: {
2418+
character: 9,
2419+
line: 5
2420+
},
2421+
start: {
2422+
character: 0,
2423+
line: 3
2424+
}
24362425
}
2437-
}
2438-
}]
2426+
}]
2427+
}
24392428
}
2440-
}
2429+
});
24412430
});
2442-
} as any);
2443-
} as any);
2431+
});
2432+
});
24442433

24452434
describe('Special file names', function (this: TestContext) {
24462435

0 commit comments

Comments
 (0)