-
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.
* CodeLense for Shebangs * added shebangCodeLens unittests * fixes travis * Changed CodeLens title * make use of async and await
- Loading branch information
1 parent
ba2ccab
commit 14ef606
Showing
7 changed files
with
144 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"use strict"; | ||
import * as vscode from 'vscode' | ||
import { TextDocument, CodeLens, CancellationToken } from 'vscode' | ||
|
||
export class ShebangCodeLensProvider implements vscode.CodeLensProvider { | ||
private settings; | ||
|
||
// reload codeLenses on every configuration change. | ||
onDidChangeCodeLenses: vscode.Event<void> = vscode.workspace.onDidChangeConfiguration; | ||
|
||
public provideCodeLenses(document: TextDocument, token: CancellationToken): Thenable<CodeLens[]> { | ||
this.settings = vscode.workspace.getConfiguration('python'); | ||
const codeLenses = this.createShebangCodeLens(document); | ||
|
||
return Promise.resolve(codeLenses); | ||
} | ||
|
||
private createShebangCodeLens(document: TextDocument) { | ||
const shebang = ShebangCodeLensProvider.detectShebang(document) | ||
if (!shebang || shebang === this.settings.get('pythonPath')) { | ||
// no shebang detected or interpreter is already set to shebang | ||
return; | ||
} | ||
|
||
// create CodeLens | ||
const firstLine = document.lineAt(0); | ||
const startOfShebang = new vscode.Position(0, 0); | ||
const endOfShebang = new vscode.Position(0, firstLine.text.length - 1); | ||
const shebangRange = new vscode.Range(startOfShebang, endOfShebang); | ||
|
||
const cmd : vscode.Command = { | ||
command: 'python.setShebangInterpreter', | ||
title: 'Set as interpreter' | ||
} | ||
|
||
const codeLenses = [(new CodeLens(shebangRange, cmd))]; | ||
return codeLenses; | ||
} | ||
|
||
public static detectShebang(document: TextDocument) { | ||
let error = false; | ||
|
||
let firstLine = document.lineAt(0); | ||
if (firstLine.isEmptyOrWhitespace) { | ||
error = true; | ||
} | ||
|
||
if (!error && "#!" === firstLine.text.substr(0, 2)) { | ||
// Shebang detected | ||
const shebang = firstLine.text.substr(2).trim(); | ||
return shebang; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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,69 @@ | ||
import * as assert from 'assert'; | ||
import * as path from 'path'; | ||
import * as vscode from 'vscode'; | ||
import { ShebangCodeLensProvider } from '../../client/providers/shebangCodeLensProvider' | ||
|
||
import { initialize, IS_TRAVIS, closeActiveWindows } from '../initialize'; | ||
|
||
const autoCompPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'shebang'); | ||
const fileShebang = path.join(autoCompPath, 'shebang.py'); | ||
const filePlain = path.join(autoCompPath, 'plain.py'); | ||
|
||
var settings = vscode.workspace.getConfiguration("python"); | ||
const origPythonPath = settings.get("pythonPath"); | ||
|
||
suite("Shebang detection", () => { | ||
suiteSetup(async () => { | ||
await initialize(); | ||
}); | ||
|
||
suiteTeardown(async () => { | ||
await vscode.workspace.getConfiguration("python").update("pythonPath", origPythonPath); | ||
}); | ||
|
||
teardown(() => closeActiveWindows()); | ||
setup(() => { | ||
settings = vscode.workspace.getConfiguration("python"); | ||
}); | ||
|
||
test("Shebang available, CodeLens showing", async () => { | ||
await settings.update("pythonPath", "python"); | ||
const editor = await openFile(fileShebang); | ||
const codeLenses = await setupCodeLens(editor); | ||
|
||
assert.equal(codeLenses.length, 1, "No CodeLens available"); | ||
let codeLens = codeLenses[0]; | ||
assert(codeLens.range.isSingleLine, 'Invalid CodeLens Range'); | ||
assert.equal(codeLens.command.command, 'python.setShebangInterpreter'); | ||
|
||
}); | ||
|
||
test("Shebang available, CodeLens hiding", async () => { | ||
await settings.update("pythonPath", "/usr/bin/test"); | ||
const editor = await openFile(fileShebang); | ||
const codeLenses = await setupCodeLens(editor); | ||
assert(!codeLenses, "CodeLens available although interpreters are equal"); | ||
|
||
}); | ||
|
||
test("Shebang missing, CodeLens hiding", async () => { | ||
const editor = await openFile(filePlain); | ||
const codeLenses = await setupCodeLens(editor); | ||
assert(!codeLenses, "CodeLens available although no shebang"); | ||
|
||
}); | ||
|
||
async function openFile(fileName: string) { | ||
const document = await vscode.workspace.openTextDocument(fileName); | ||
const editor = await vscode.window.showTextDocument(document); | ||
assert(vscode.window.activeTextEditor, 'No active editor'); | ||
return editor; | ||
} | ||
|
||
async function setupCodeLens(editor: vscode.TextEditor) { | ||
const document = editor.document; | ||
const codeLensProvider = new ShebangCodeLensProvider(); | ||
const codeLenses = await codeLensProvider.provideCodeLenses(document, null); | ||
return codeLenses; | ||
} | ||
}); |
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,2 @@ | ||
|
||
print("dummy") |
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,3 @@ | ||
#!/usr/bin/test | ||
|
||
print("dummy") |