Skip to content

Commit eb5108b

Browse files
committed
fix #1115
1 parent fa96749 commit eb5108b

6 files changed

Lines changed: 51 additions & 32 deletions

File tree

src/client/extension.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function activate(context: vscode.ExtensionContext) {
8989
},
9090
{
9191
beforeText: /^\s+(continue|break|return)\b.*$/,
92-
action: {indentAction: vscode.IndentAction.Outdent},
92+
action: { indentAction: vscode.IndentAction.Outdent },
9393
}
9494
]
9595
});
@@ -102,7 +102,8 @@ export function activate(context: vscode.ExtensionContext) {
102102
context.subscriptions.push(vscode.languages.registerReferenceProvider(PYTHON, new PythonReferenceProvider(context, jediProx)));
103103
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(PYTHON, new PythonCompletionItemProvider(context, jediProx), '.'));
104104

105-
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(PYTHON, new PythonSymbolProvider(context, jediProx)));
105+
const symbolProvider = new PythonSymbolProvider(context, jediProx);
106+
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(PYTHON, symbolProvider));
106107
if (pythonSettings.devOptions.indexOf('DISABLE_SIGNATURE') === -1) {
107108
context.subscriptions.push(vscode.languages.registerSignatureHelpProvider(PYTHON, new PythonSignatureProvider(context, jediProx), '(', ','));
108109
}
@@ -133,7 +134,7 @@ export function activate(context: vscode.ExtensionContext) {
133134
context.subscriptions.push(jupMain);
134135
linterProvider.documentHasJupyterCodeCells = documentHasJupyterCodeCells;
135136
}
136-
tests.activate(context, unitTestOutChannel);
137+
tests.activate(context, unitTestOutChannel, symbolProvider);
137138

138139
context.subscriptions.push(new WorkspaceSymbols(lintingOutChannel));
139140

src/client/providers/jediProxy.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,6 @@ export interface IHoverItem {
666666

667667
export class JediProxyHandler<R extends ICommandResult> {
668668
private jediProxy: JediProxy;
669-
private lastToken: vscode.CancellationToken;
670-
private lastCommandId: number;
671669
private cancellationTokenSource: vscode.CancellationTokenSource;
672670

673671
public get JediProxy(): JediProxy {
@@ -680,7 +678,6 @@ export class JediProxyHandler<R extends ICommandResult> {
680678

681679
public sendCommand(cmd: ICommand<R>, token?: vscode.CancellationToken): Promise<R> {
682680
var executionCmd = <IExecutionCommand<R>>cmd;
683-
const def = createDeferred<R>();
684681
executionCmd.id = executionCmd.id || this.jediProxy.getNextCommandId();
685682

686683
if (this.cancellationTokenSource) {
@@ -692,23 +689,23 @@ export class JediProxyHandler<R extends ICommandResult> {
692689

693690
this.cancellationTokenSource = new vscode.CancellationTokenSource();
694691
executionCmd.token = this.cancellationTokenSource.token;
695-
this.lastToken = token;
696-
this.lastCommandId = executionCmd.id;
697692

698-
this.jediProxy.sendCommand<R>(executionCmd).then(data => {
699-
if (this.lastToken.isCancellationRequested || !data || data.requestId !== this.lastCommandId) {
700-
def.resolve();
701-
}
702-
if (data) {
703-
def.resolve(data);
704-
}
705-
else {
706-
def.resolve();
707-
}
708-
}).catch(reason => {
709-
console.error(reason);
710-
def.resolve();
711-
});
712-
return def.promise;
693+
return this.jediProxy.sendCommand<R>(executionCmd)
694+
.catch(reason => {
695+
console.error(reason);
696+
return undefined;
697+
});
698+
}
699+
700+
public sendCommandNonCancellableCommand(cmd: ICommand<R>, token?: vscode.CancellationToken): Promise<R> {
701+
var executionCmd = <IExecutionCommand<R>>cmd;
702+
executionCmd.id = executionCmd.id || this.jediProxy.getNextCommandId();
703+
executionCmd.token = token;
704+
705+
return this.jediProxy.sendCommand<R>(executionCmd)
706+
.catch(reason => {
707+
console.error(reason);
708+
return undefined;
709+
});
713710
}
714711
}

src/client/providers/symbolProvider.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import * as vscode from 'vscode';
44
import * as proxy from './jediProxy';
5-
import * as telemetryContracts from "../common/telemetryContracts";
65

76
export class PythonSymbolProvider implements vscode.DocumentSymbolProvider {
87
private jediProxyHandler: proxy.JediProxyHandler<proxy.ISymbolResult>;
@@ -43,4 +42,22 @@ export class PythonSymbolProvider implements vscode.DocumentSymbolProvider {
4342
return PythonSymbolProvider.parseData(document, data);
4443
});
4544
}
45+
public provideDocumentSymbolsForInternalUse(document: vscode.TextDocument, token: vscode.CancellationToken): Thenable<vscode.SymbolInformation[]> {
46+
var filename = document.fileName;
47+
48+
var cmd: proxy.ICommand<proxy.ISymbolResult> = {
49+
command: proxy.CommandType.Symbols,
50+
fileName: filename,
51+
columnIndex: 0,
52+
lineIndex: 0
53+
};
54+
55+
if (document.isDirty) {
56+
cmd.source = document.getText();
57+
}
58+
59+
return this.jediProxyHandler.sendCommandNonCancellableCommand(cmd, token).then(data => {
60+
return PythonSymbolProvider.parseData(document, data);
61+
});
62+
}
4663
}

src/client/unittests/codeLenses/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import * as vscode from 'vscode';
22
import * as constants from '../../common/constants';
33

44
import { TestFileCodeLensProvider } from './testFiles';
5+
import { PythonSymbolProvider } from '../../providers/symbolProvider';
56

6-
export function activateCodeLenses(onDidChange: vscode.EventEmitter<void>): vscode.Disposable {
7+
export function activateCodeLenses(onDidChange: vscode.EventEmitter<void>, symboldProvider: PythonSymbolProvider): vscode.Disposable {
78
const disposables: vscode.Disposable[] = [];
8-
disposables.push(vscode.languages.registerCodeLensProvider(constants.PythonLanguage, new TestFileCodeLensProvider(onDidChange)));
9+
disposables.push(vscode.languages.registerCodeLensProvider(constants.PythonLanguage, new TestFileCodeLensProvider(onDidChange, symboldProvider)));
910

1011
return {
1112
dispose: function () {

src/client/unittests/codeLenses/testFiles.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CodeLensProvider, TextDocument, CancellationToken, CodeLens, SymbolInfo
55
import { TestFile, TestsToRun, TestSuite, TestFunction, TestStatus } from '../common/contracts';
66
import * as constants from '../../common/constants';
77
import { getDiscoveredTests } from '../common/testUtils';
8+
import { PythonSymbolProvider } from '../../providers/symbolProvider';
89

910
interface CodeLensData {
1011
symbolKind: vscode.SymbolKind;
@@ -17,7 +18,7 @@ interface FunctionsAndSuites {
1718
}
1819

1920
export class TestFileCodeLensProvider implements CodeLensProvider {
20-
constructor(private _onDidChange: vscode.EventEmitter<void>) {
21+
constructor(private _onDidChange: vscode.EventEmitter<void>, private symbolProvider: PythonSymbolProvider) {
2122
}
2223

2324
get onDidChangeCodeLenses(): vscode.Event<void> {
@@ -41,7 +42,7 @@ export class TestFileCodeLensProvider implements CodeLensProvider {
4142
}
4243
}, constants.Delays.MaxUnitTestCodeLensDelay);
4344

44-
return getCodeLenses(document.uri, token);
45+
return getCodeLenses(document, token, this.symbolProvider);
4546
}
4647

4748
resolveCodeLens(codeLens: CodeLens, token: CancellationToken): CodeLens | Thenable<CodeLens> {
@@ -50,7 +51,8 @@ export class TestFileCodeLensProvider implements CodeLensProvider {
5051
}
5152
}
5253

53-
function getCodeLenses(documentUri: vscode.Uri, token: vscode.CancellationToken): Thenable<CodeLens[]> {
54+
function getCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken, symbolProvider: PythonSymbolProvider): Thenable<CodeLens[]> {
55+
const documentUri = document.uri;
5456
const tests = getDiscoveredTests();
5557
if (!tests) {
5658
return null;
@@ -61,7 +63,7 @@ function getCodeLenses(documentUri: vscode.Uri, token: vscode.CancellationToken)
6163
}
6264
const allFuncsAndSuites = getAllTestSuitesAndFunctionsPerFile(file);
6365

64-
return vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', documentUri, token)
66+
return symbolProvider.provideDocumentSymbolsForInternalUse(document, token)
6567
.then((symbols: vscode.SymbolInformation[]) => {
6668
return symbols.filter(symbol => {
6769
return symbol.kind === vscode.SymbolKind.Function ||

src/client/unittests/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { TestDisplay } from './display/picker';
1212
import * as constants from '../common/constants';
1313
import { activateCodeLenses } from './codeLenses/main';
1414
import { displayTestFrameworkError } from './configuration';
15+
import { PythonSymbolProvider } from '../providers/symbolProvider';
1516

1617
const settings = PythonSettings.getInstance();
1718
let testManager: BaseTestManager;
@@ -23,7 +24,7 @@ let testDisplay: TestDisplay;
2324
let outChannel: vscode.OutputChannel;
2425
let onDidChange: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
2526

26-
export function activate(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel) {
27+
export function activate(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel, symboldProvider: PythonSymbolProvider) {
2728
context.subscriptions.push({ dispose: dispose });
2829
outChannel = outputChannel;
2930
let disposables = registerCommands();
@@ -38,7 +39,7 @@ export function activate(context: vscode.ExtensionContext, outputChannel: vscode
3839
}
3940

4041
settings.addListener('change', onConfigChanged);
41-
context.subscriptions.push(activateCodeLenses(onDidChange));
42+
context.subscriptions.push(activateCodeLenses(onDidChange, symboldProvider));
4243
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(onDocumentSaved));
4344
}
4445

0 commit comments

Comments
 (0)