Skip to content

Commit

Permalink
Merge pull request 91 from egamma/api-update into master
Browse files Browse the repository at this point in the history
  • Loading branch information
egamma committed Sep 25, 2015
2 parents b2cb577 + 8e96786 commit ba2e6db
Show file tree
Hide file tree
Showing 12 changed files with 690 additions and 973 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"runtimeExecutable": "${execPath}",
// force VSCode to create a new window and pass this project as an additonal plugin location
// add workspace path to %GOPATH% so that there is some meaningful contents
"args": [ "--new-window", "--extensionDevelopmentWorkspacePath=${workspaceRoot}" ],
"args": [ "--new-window", "--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "out"
Expand Down
108 changes: 87 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,88 @@
{
"name": "go-code",
"version": "1.0.0",
"description": "Go support for Visual Studio Code",
"author": {
"name": "Microsoft Corporation"
},
"private": true,
"repository": {
"type": "git",
"url": "https://monacotools.visualstudio.com/DefaultCollection/Monaco/_git/go-code"
},
"scripts": {
"prepublish": "tsc"
},
"dependencies": {
"json-rpc2": "^1.0.2"
},
"devDependencies": {
"typescript": "^1.6.2"
}
}
"name": "go-code",
"version": "1.0.0",
"description": "Go support for Visual Studio Code",
"author": {
"name": "Microsoft Corporation"
},
"private": true,
"repository": {
"type": "git",
"url": "https://monacotools.visualstudio.com/DefaultCollection/Monaco/_git/go-code"
},
"scripts": {
"prepublish": "tsc"
},
"extensionDependencies": [
"go"
],
"dependencies": {
"json-rpc2": "^1.0.2"
},
"devDependencies": {
"typescript": "^1.6.2"
},
"engines": {
"vscode": "*"
},
"activationEvents": [
"textModel:go"
],
"main": "./out/goMain",
"contributes": {
"language":
[{
"id": "go",
"extensions": [
".go"
],
"aliases": [
"Go"
]
}
],
"debugAdapter": [
{
"type": "go",
"enableBreakpointsFor": {
"languageIds": [
"go"
]
},
"program": "./out/debugAdapter/openDebugMock.js",
"runtime": "node"
}
],
"configuration": {
"type": "object",
"title": "Go configuration",
"properties": {
"go.buildOnSave": {
"type": "boolean",
"default": true,
"description": "Run 'go build'/'go test' on save."
},
"go.lintOnSave": {
"type": "boolean",
"default": true,
"description": "Run 'golint' on save."
},
"go.vetOnSave": {
"type": "boolean",
"default": true,
"description": "Run 'go tool vet' on save."
},
"go.formatTool": {
"type": "string",
"default": "goreturns",
"description": "Pick 'gofmt', 'goimports' or 'goreturns' to run on format."
},
"go.gopath": {
"type": "string",
"default": null,
"description": "Specifies the GOPATH to use when no environment variable is set."
}
}
}
}
}
8 changes: 4 additions & 4 deletions src/goCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if(process.env.GOROOT) {
go = pathparts.map(dir => path.join(dir, 'go' + (os.platform() == "win32" ? ".exe" : ""))).filter(candidate => fs.existsSync(candidate))[0];
}
if(!go) {
vscode.shell.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT. Set location manual in 'go.goroot' setting.");
vscode.window.showInformationMessage("No 'go' binary could be found on PATH or in GOROOT. Set location manual in 'go.goroot' setting.");
}

export interface ICheckResult {
Expand All @@ -40,7 +40,7 @@ export function check(filename: string, buildOnSave = true, lintOnSave = true, v
cp.execFile(go, args, {cwd: cwd}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'go' compiler is not available. Install Go from http://golang.org/dl/.");
vscode.window.showInformationMessage("The 'go' compiler is not available. Install Go from http://golang.org/dl/.");
return resolve([]);
}
var lines = stderr.toString().split('\n');
Expand All @@ -65,7 +65,7 @@ export function check(filename: string, buildOnSave = true, lintOnSave = true, v
cp.execFile(golint, [filename], {cwd: cwd}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'golint' command is not available. Use 'go get -u github.com/golang/lint/golint' to install.");
vscode.window.showInformationMessage("The 'golint' command is not available. Use 'go get -u github.com/golang/lint/golint' to install.");
return resolve([]);
}
var lines = stdout.toString().split('\n');
Expand All @@ -89,7 +89,7 @@ export function check(filename: string, buildOnSave = true, lintOnSave = true, v
cp.execFile(go, ["tool", "vet", filename], {cwd: cwd}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'go tool vet' compiler is not available. Install Go from http://golang.org/dl/.");
vscode.window.showInformationMessage("The 'go tool vet' compiler is not available. Install Go from http://golang.org/dl/.");
return resolve([]);
}
var lines = stdout.toString().split('\n');
Expand Down
35 changes: 10 additions & 25 deletions src/goDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,23 @@ import path = require('path');

class DeclartionSupport implements vscode.Modes.IDeclarationSupport {

private modelService: vscode.Services.IModelService;

constructor(modelService: vscode.Services.IModelService) {
this.modelService = modelService;
}

public findDeclaration(resource:vscode.Uri, position:vscode.IPosition, token: vscode.CancellationToken):Promise<vscode.Modes.IReference> {
public findDeclaration(document:vscode.TextDocument, position:vscode.Position, token: vscode.CancellationToken):Thenable<vscode.Modes.IReference> {

return new Promise((resolve, reject) => {
var filename = resource.fsPath;
var model = this.modelService.getModel(resource);
var wordAtPosition = model.getWordAtPosition(position);

var wordAtPosition = document.getWordRangeAtPosition(position);

// compute the file offset for position
var offset = model.getValueInRange({
startLineNumber: 0,
startColumn: 0,
endLineNumber: position.lineNumber,
endColumn: position.column
}).length;
var range = new vscode.Range(0, 0, position.line, position.column);
var offset = document.getTextInRange(range).length;

var godef = path.join(process.env["GOPATH"], "bin", "godef");

// Spawn `godef` process
var p = cp.execFile(godef, ["-t", "-i", "-f", filename, "-o", offset.toString()], {}, (err, stdout, stderr) => {
var p = cp.execFile(godef, ["-t", "-i", "-f", document.getUri().fsPath, "-o", offset.toString()], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'godef' command is not available. Use 'go get -u github.com/rogpeppe/godef' to install.");
vscode.window.showInformationMessage("The 'godef' command is not available. Use 'go get -u github.com/rogpeppe/godef' to install.");
}
if (err) return resolve(null);
var result = stdout.toString();
Expand All @@ -48,20 +37,16 @@ class DeclartionSupport implements vscode.Modes.IDeclarationSupport {
if(!match) return resolve(null);
var [_, file, line, col] = match;
var definitionResource = vscode.Uri.file(file);
var range = new vscode.Range(+line, +col, +line, +col + 1);
return resolve({
resource: definitionResource,
range: {
startLineNumber: +line,
startColumn: +col,
endLineNumber: +line,
endColumn: +col + 1
}
range
});
} catch(e) {
reject(e);
}
});
p.stdin.end(model.getValue());
p.stdin.end(document.getText());
});
}
}
Expand Down
36 changes: 13 additions & 23 deletions src/goExtraInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,46 @@ import path = require('path');

class ExtraInfoSupport implements vscode.Modes.IExtraInfoSupport {

private modelService: vscode.Services.IModelService;

constructor(modelService: vscode.Services.IModelService) {
this.modelService = modelService;
}

public computeInfo(resource:vscode.Uri, position:vscode.IPosition, token: vscode.CancellationToken): Promise<vscode.Modes.IComputeExtraInfoResult> {
public computeInfo(document:vscode.TextDocument, position:vscode.Position, token: vscode.CancellationToken): Promise<vscode.Modes.IComputeExtraInfoResult> {

return new Promise((resolve, reject) => {
var filename = resource.fsPath;
var model = this.modelService.getModel(resource);
var wordAtPosition = model.getWordAtPosition(position);
var filename = document.getUri().fsPath;
var wordAtPosition = document.getWordRangeAtPosition(position);

// compute the file offset for position
var offset = model.getValueInRange({
startLineNumber: 0,
startColumn: 0,
endLineNumber: position.lineNumber,
endColumn: position.column
}).length;
var range = new vscode.Range(0, 0, position.line, position.column);
var offset = document.getTextInRange(range).length;

var godef = path.join(process.env["GOPATH"], "bin", "godef");

// Spawn `godef` process
var p = cp.execFile(godef, ["-t", "-i", "-f", filename, "-o", offset.toString()], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'godef' command is not available. Use 'go get -u github.com/rogpeppe/godef' to install.");
vscode.window.showInformationMessage("The 'godef' command is not available. Use 'go get -u github.com/rogpeppe/godef' to install.");
}
if (err) return resolve(null);
var result = stdout.toString();
var lines = result.split('\n');
if(lines.length > 10) lines[9] = "...";
var text = lines.slice(1,10).join('\n');
var range = new vscode.Range(
position.line,
wordAtPosition ? wordAtPosition.start.column : position.column,
position.line,
wordAtPosition ? wordAtPosition.end.column : position.column);
return resolve({
htmlContent: [
{ formattedText: text }
],
range: {
startLineNumber: position.lineNumber,
startColumn: wordAtPosition ? wordAtPosition.startColumn : position.column,
endLineNumber: position.lineNumber,
endColumn: wordAtPosition ? wordAtPosition.endColumn : position.column
}
range
});
} catch(e) {
reject(e);
}
});
p.stdin.end(model.getValue());
p.stdin.end(document.getText());
});
}
}
Expand Down
47 changes: 20 additions & 27 deletions src/goFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,52 @@ import path = require('path');

class FormattingSupport implements vscode.Modes.IFormattingSupport {

private modelService: vscode.Services.IModelService;
private formatCommand = "goreturns";

public autoFormatTriggerCharacters: string[] = [';', '}', '\n'];

constructor(modelService: vscode.Services.IModelService, configurationService: vscode.Services.IConfigurationService) {
this.modelService = modelService;
configurationService.loadConfiguration('go').then(config => {
if(config.formatTool) {
this.formatCommand = config.formatTool;
constructor() {
vscode.extensions.getConfigurationMemento('go').getValue<string>('formatTool').then(formatTool => {
if(formatTool) {
this.formatCommand = formatTool;
}
});
}

public formatDocument(resource: vscode.Uri, options: vscode.Modes.IFormattingOptions, token: vscode.CancellationToken):Thenable<vscode.Models.ISingleEditOperation[]> {
// TODO: We don't really need to save all the buffers, just the one for 'resource'.
return vscode.workspace.anyDirty().then(anyDirty => {
if (anyDirty) {
vscode.workspace.saveAll(false).then(() => {
return this.doFormatDocument(resource, options, token);
});
}
return this.doFormatDocument(resource, options, token);
// TODO: work around bug that Code always calls formatRange
public formatRange(document: vscode.TextDocument, range: vscode.Range, options: vscode.Modes.IFormattingOptions, token: vscode.CancellationToken): Thenable<vscode.Modes.ISingleEditOperation[]> {
return this.formatDocument(document, options, token)
}

public formatDocument(document: vscode.TextDocument, options: vscode.Modes.IFormattingOptions, token: vscode.CancellationToken): Thenable<vscode.Modes.ISingleEditOperation[]> {
// TODO: We don't really need to save all the buffers, just the one for 'resource.
return vscode.workspace.saveAll(false).then(() => {
return this.doFormatDocument(document, options, token);
});
}

private doFormatDocument(resource: vscode.Uri, options: vscode.Modes.IFormattingOptions, token: vscode.CancellationToken):Thenable<vscode.Models.ISingleEditOperation[]> {
private doFormatDocument(document: vscode.TextDocument, options: vscode.Modes.IFormattingOptions, token: vscode.CancellationToken):Thenable<vscode.Modes.ISingleEditOperation[]> {
return new Promise((resolve, reject) => {
var filename = resource.fsPath;
var model = this.modelService.getModel(resource);
var filename = document.getUri().fsPath;

var goreturns = path.join(process.env["GOPATH"], "bin", this.formatCommand);

cp.execFile(goreturns, [filename], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.shell.showInformationMessage("The 'goreturns' command is not available. Use 'go get -u sourcegraph.com/sqs/goreturns' to install.");
vscode.window.showInformationMessage("The 'goreturns' command is not available. Use 'go get -u sourcegraph.com/sqs/goreturns' to install.");
return resolve(null);
}
if (err) return reject("Cannot format due to syntax errors.");
var result = stdout.toString();
// TODO: Should use `-d` option to get a diff and then compute the
// specific edits instead of replace whole buffer
var lastLine = model.getLineCount();
var lastLineLastCol = model.getLineMaxColumn(lastLine);
var lastLine = document.getLineCount();
var lastLineLastCol = document.getLineMaxColumn(lastLine);
var range = new vscode.Range(1, 1, lastLine, lastLineLastCol);
return resolve([{
text: result,
range: {
startLineNumber: 1,
startColumn: 1,
endLineNumber: lastLine,
endColumn: lastLineLastCol
}
range
}]);
} catch(e) {
reject(e);
Expand Down
Loading

0 comments on commit ba2e6db

Please sign in to comment.