-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/DonJayamanne/pythonVSCode
* 'master' of https://github.com/DonJayamanne/pythonVSCode: Fix #996 async with EXPR as VAR (#1108) fix changelog typo (#1107) #764 feature request: auto import (#1032)
- Loading branch information
Showing
11 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
import * as vscode from 'vscode'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { AutoImportProvider } from './providers/autoImportProvider'; | ||
|
||
|
||
export function activate(context: vscode.ExtensionContext, outChannel: vscode.OutputChannel) { | ||
let extension = new AutoImportProvider(); | ||
context.subscriptions.push(vscode.commands.registerCommand('python.autoImportAtCursor', extension.autoImportAtCursor)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as vscode from 'vscode'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
export class AutoImportProvider { | ||
|
||
constructor() { | ||
} | ||
|
||
autoImportAtCursor() { | ||
let ed = vscode.window.activeTextEditor; | ||
let range : vscode.Range = ed.selection; | ||
|
||
if(range.start.line !== range.end.line) { | ||
return; | ||
} | ||
|
||
if(range.start.character === range.end.character) { | ||
range = ed.document.getWordRangeAtPosition(range.end); | ||
} | ||
const symbol = vscode.window.activeTextEditor.document.getText(range); | ||
|
||
if(!symbol) { | ||
return; | ||
} | ||
|
||
return this.autoImport(symbol); | ||
} | ||
|
||
async autoImport(symbol: string) { | ||
let editor = vscode.window.activeTextEditor; | ||
let result = await this.lookupSymbol(symbol); | ||
|
||
result = result.filter((s: vscode.SymbolInformation) => | ||
s.name === symbol && // Only exact and case sensitive matches should be considered for import | ||
s.kind !== vscode.SymbolKind.Namespace // only declarations should be considered for import | ||
); | ||
|
||
if(result.length === 0) { | ||
vscode.window.showInformationMessage('No matching symbols found'); | ||
return; | ||
} else { | ||
var import_choices: string[] = result.map( | ||
s => `from ${pathAsPyModule(s.location)} import ${s.name}` | ||
); | ||
|
||
let s = await this.showChoices(import_choices); | ||
if(s) { | ||
return addImport(editor, s); | ||
} | ||
} | ||
} | ||
|
||
lookupSymbol(symbol: string) { | ||
return <Promise<vscode.SymbolInformation[]>> | ||
vscode.commands.executeCommand('vscode.executeWorkspaceSymbolProvider', symbol); | ||
} | ||
|
||
showChoices(import_choices: string[]) { | ||
return vscode.window.showQuickPick(import_choices); | ||
} | ||
} | ||
|
||
export function pathAsPyModule(l: vscode.Location): string { | ||
var pymodule = path.basename(l.uri.fsPath).replace(/\.py$/, ''); | ||
var location = path.dirname(l.uri.fsPath); | ||
while(fs.existsSync(path.join(location, '__init__.py'))) { | ||
pymodule = path.basename(location) + '.' + pymodule; | ||
location = path.dirname(location); | ||
} | ||
return pymodule; | ||
} | ||
|
||
export function addImport(ed: vscode.TextEditor, import_string: string) { | ||
return ed.edit((b: vscode.TextEditorEdit) => b.insert( | ||
getPositionForNewImport(import_string), | ||
import_string + '\n' | ||
)); | ||
} | ||
|
||
|
||
export function getPositionForNewImport(import_string: string): vscode.Position { | ||
// TODO: figure out better position: | ||
return new vscode.Position(0, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import { AutoImportProvider } from './../client/providers/autoImportProvider'; | ||
|
||
let testPath = path.join(__dirname, '../../src/test'); | ||
const fileOne = path.join(testPath, 'pythonFiles/autoimport/one.py'); | ||
const fileThree = path.join(testPath, 'pythonFiles/autoimport/two/three.py'); | ||
|
||
let fileOneLoc = new vscode.Location(vscode.Uri.file(fileOne), new vscode.Position(0, 0)); | ||
let fileThreeLoc = new vscode.Location(vscode.Uri.file(fileThree), new vscode.Position(0, 0)); | ||
|
||
|
||
suite('Autoimport', () => { | ||
let autoimp = new AutoImportProvider(); | ||
let lookupSymbol = sinon.stub(autoimp, 'lookupSymbol'); | ||
let showChoices = sinon.stub(autoimp, 'showChoices'); | ||
|
||
suiteSetup(() => { | ||
}); | ||
|
||
setup(() => { | ||
lookupSymbol.reset(); | ||
showChoices.reset(); | ||
return vscode.workspace.openTextDocument(fileOne).then(vscode.window.showTextDocument); | ||
}); | ||
|
||
test('choices are filtered and displayed"', done => { | ||
lookupSymbol.resolves([ | ||
new vscode.SymbolInformation( | ||
'TestClass', vscode.SymbolKind.Class, '', fileOneLoc | ||
), | ||
new vscode.SymbolInformation( | ||
'TestClassTwo', vscode.SymbolKind.Class, '', fileOneLoc | ||
), | ||
new vscode.SymbolInformation( | ||
'TestClass', vscode.SymbolKind.Namespace, '', fileOneLoc | ||
), | ||
]); | ||
|
||
showChoices.resolves(undefined); | ||
|
||
autoimp.autoImport('TestClass').then(function() { | ||
assert(lookupSymbol.calledWith('TestClass')); | ||
assert(showChoices.calledWith(['from one import TestClass'])); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('module under a package"', done => { | ||
lookupSymbol.resolves([ | ||
new vscode.SymbolInformation( | ||
'TestClass', vscode.SymbolKind.Class, '', fileThreeLoc | ||
), | ||
]); | ||
|
||
showChoices.resolves(undefined); | ||
|
||
autoimp.autoImport('TestClass').then(function() { | ||
assert(showChoices.calledWith(['from two.three import TestClass'])); | ||
done(); | ||
}); | ||
}); | ||
|
||
test('selection is added at line one', done => { | ||
lookupSymbol.resolves([ | ||
new vscode.SymbolInformation( | ||
'TestClass', vscode.SymbolKind.Class, '', fileOneLoc | ||
), | ||
]); | ||
|
||
showChoices.resolves('from one import TestClass'); | ||
|
||
autoimp.autoImport('TestClass').then(function() { | ||
let line0 = vscode.window.activeTextEditor.document.lineAt(0); | ||
assert.equal(line0.text, 'from one import TestClass'); | ||
done(); | ||
}); | ||
}); | ||
}); |
Empty file.
Empty file.
Empty file.