diff --git a/extensions/typescript-language-features/src/languageFeatures/documentSymbol.ts b/extensions/typescript-language-features/src/languageFeatures/documentSymbol.ts index ef2d47ef942..349c04c3048 100644 --- a/extensions/typescript-language-features/src/languageFeatures/documentSymbol.ts +++ b/extensions/typescript-language-features/src/languageFeatures/documentSymbol.ts @@ -33,7 +33,7 @@ const getSymbolKind = (kind: string): vscode.SymbolKind => { return vscode.SymbolKind.Variable; }; -class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider { +export class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider { public constructor( private readonly client: ITypeScriptServiceClient, @@ -60,7 +60,7 @@ class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider return result; } - private static convertNavTree( + public static convertNavTree( resource: vscode.Uri, output: vscode.DocumentSymbol[], item: Proto.NavigationTree, diff --git a/extensions/typescript-language-features/src/languageFeatures/quickFix.ts b/extensions/typescript-language-features/src/languageFeatures/quickFix.ts index b83af8d2526..f838fb4e4eb 100644 --- a/extensions/typescript-language-features/src/languageFeatures/quickFix.ts +++ b/extensions/typescript-language-features/src/languageFeatures/quickFix.ts @@ -35,6 +35,21 @@ class ApplyCodeActionCommand implements Command { private readonly telemetryReporter: TelemetryReporter, ) { } + private findEndLine(startLine: number, navigationTree: Proto.NavigationTree[]): number { + + for (const node of navigationTree) { + const nodeStartLine = node.spans[0].start.line; + const nodeEndLine = node.spans[0].end.line; + + if (nodeStartLine === startLine) { + return nodeEndLine; + } else if (startLine > nodeStartLine && startLine <= nodeEndLine && node.childItems) { + return this.findEndLine(startLine, node.childItems); + } + } + return -1; + } + public async execute({ resource, action, diagnostic }: ApplyCodeActionCommand_args): Promise { /* __GDPR__ "quickFix.execute" : { @@ -45,33 +60,39 @@ class ApplyCodeActionCommand implements Command { ] } */ - console.log('resource : ', resource); - console.log('action : ', action); - console.log('diagnostic : ', diagnostic); this.telemetryReporter.logTelemetry('quickFix.execute', { fixName: action.fixName }); this.diagnosticManager.deleteDiagnostic(resource, diagnostic); - // TODO: point of interest const codeActionResult = await applyCodeActionCommands(this.client, action.commands, nulToken); - // TODO: how to find the end line number of the class? - // const startLine = diagnostic.range.start.line; - // Find end line using the outline - // vscode.window.activeTextEditor?.document - - const numberLines = vscode.window.activeTextEditor?.document.lineCount; - - // once done, need to call the interactive editor, when working with the implentation interface case if (action.fixName === 'fixClassIncorrectlyImplementsInterface') { - console.log('case when class incorrectly implements interface'); - await vscode.commands.executeCommand('interactiveEditor.start', { initialRange: { startLineNumber: 0, endLineNumber: numberLines, startColumn: 0, endColumn: 0 }, message: 'Implement the class from the interface', autoSend: true }); - } - // - return codeActionResult; + const diagnosticStartLine = diagnostic.range.start.line + 1; + const document = vscode.window.activeTextEditor?.document; + + if (document) { + const filepath = this.client.toOpenTsFilePath(document); + + if (filepath) { + const cancellationTokenSource = new vscode.CancellationTokenSource(); + const response = await this.client.execute('navtree', { file: filepath }, cancellationTokenSource.token); + + if (response.type === 'response' && response.body?.childItems) { + + const endLineFound = this.findEndLine(diagnosticStartLine, response.body.childItems); + const endLine = endLineFound !== -1 ? endLineFound : vscode.window.activeTextEditor?.document.lineCount; + const startLine = endLineFound !== -1 ? diagnosticStartLine : 0; + + await vscode.commands.executeCommand('interactiveEditor.start', { initialRange: { startLineNumber: startLine, endLineNumber: endLine, startColumn: 0, endColumn: 0 }, message: 'Implement the class using the interface', autoSend: true }); + } + } + } + return codeActionResult; + } + return false; } }