Skip to content

Commit

Permalink
update to 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
egamma committed Oct 28, 2015
1 parent 4f0a814 commit e9f0bae
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 152 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"json-rpc2": "^1.0.2"
},
"devDependencies": {
"vscode": "*"
"vscode": "next"
},
"engines": {
"vscode": "*"
Expand Down
15 changes: 6 additions & 9 deletions src/goDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');

class DeclartionSupport implements vscode.Modes.IDeclarationSupport {
export class GoDefinitionProvider implements vscode.DefinitionProvider {

public findDeclaration(document:vscode.TextDocument, position:vscode.Position, token: vscode.CancellationToken):Thenable<vscode.Modes.IReference> {
public provideDefinition(document:vscode.TextDocument, position:vscode.Position, token: vscode.CancellationToken):Thenable<vscode.Location> {

return new Promise((resolve, reject) => {

Expand All @@ -37,11 +37,10 @@ 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
});
var range = new vscode.Range(+line-1, +col-1, +line-1, +col-1);
return resolve(
new vscode.Location(definitionResource, range)
);
} catch(e) {
reject(e);
}
Expand All @@ -50,5 +49,3 @@ class DeclartionSupport implements vscode.Modes.IDeclarationSupport {
});
}
}

export = DeclartionSupport;
39 changes: 16 additions & 23 deletions src/goExtraInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,42 @@

'use strict';

import vscode = require('vscode');
import {window, HoverProvider, Hover, TextDocument, Position, Range, IHTMLContentElement, CancellationToken} from 'vscode';
import cp = require('child_process');
import path = require('path');

class ExtraInfoSupport implements vscode.Modes.IExtraInfoSupport {
export class GoHoverProvider implements HoverProvider {


public computeInfo(document:vscode.TextDocument, position:vscode.Position, token: vscode.CancellationToken): Promise<vscode.Modes.IComputeExtraInfoResult> {
public provideHover(document: TextDocument, position: Position, token: CancellationToken): Thenable<Hover> {

return new Promise((resolve, reject) => {
var filename = document.getUri().fsPath;
var wordAtPosition = document.getWordRangeAtPosition(position);
let filename = document.getUri().fsPath;
let wordAtPosition = document.getWordRangeAtPosition(position);

// compute the file offset for position
var range = new vscode.Range(0, 0, position.line, position.character);
var offset = document.getTextInRange(range).length;
let range = new Range(0, 0, position.line, position.character);
let offset = document.getTextInRange(range).length;

var godef = path.join(process.env["GOPATH"], "bin", "godef");
let 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) => {
let p = cp.execFile(godef, ["-t", "-i", "-f", filename, "-o", offset.toString()], {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code == "ENOENT") {
vscode.window.showInformationMessage("The 'godef' command is not available. Use 'go get -u github.com/rogpeppe/godef' to install.");
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');
let result = stdout.toString();
let lines = result.split('\n');
if(lines.length > 10) lines[9] = "...";
var text = lines.slice(1,10).join('\n');
var range = new vscode.Range(
let text = lines.slice(1,10).join('\n');
let range = new Range(
position.line,
wordAtPosition ? wordAtPosition.start.character : position.character,
position.line,
wordAtPosition ? wordAtPosition.end.character : position.character);
return resolve({
htmlContent: [
{ formattedText: text }
],
range
});
let hover = new Hover(text, range);
return resolve(hover);
} catch(e) {
reject(e);
}
Expand All @@ -53,5 +48,3 @@ class ExtraInfoSupport implements vscode.Modes.IExtraInfoSupport {
});
}
}

export = ExtraInfoSupport;
17 changes: 8 additions & 9 deletions src/goFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');

class FormattingSupport implements vscode.Modes.IFormattingSupport {
export class GoDocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {

private formatCommand = "goreturns";

// Not used?
public autoFormatTriggerCharacters: string[] = [';', '}', '\n'];

constructor() {
Expand All @@ -22,13 +23,13 @@ class FormattingSupport implements vscode.Modes.IFormattingSupport {
});
}

public formatDocument(document: vscode.TextDocument, options: vscode.Modes.IFormattingOptions, token: vscode.CancellationToken): Thenable<vscode.Modes.ISingleEditOperation[]> {
public provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<vscode.TextEdit[]> {
return document.save().then(() => {
return this.doFormatDocument(document, options, token);
});
}

private doFormatDocument(document: vscode.TextDocument, options: vscode.Modes.IFormattingOptions, token: vscode.CancellationToken):Thenable<vscode.Modes.ISingleEditOperation[]> {
private doFormatDocument(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken):Thenable<vscode.TextEdit[]> {
return new Promise((resolve, reject) => {
var filename = document.getUri().fsPath;

Expand All @@ -45,16 +46,14 @@ class FormattingSupport implements vscode.Modes.IFormattingSupport {
// TODO: Should use `-d` option to get a diff and then compute the
// specific edits instead of replace whole buffer
var lastLine = document.getLineCount();
var lastLineLastCol = document.getLineMaxColumn(lastLine);
var range = new vscode.Range(1, 1, lastLine, lastLineLastCol);
return resolve([{ text, range }]);
var lastLineLastCol = document.getLine(lastLine-1).getEnd().character;
var range = new vscode.Range(0, 0, lastLine-1, lastLineLastCol);
return resolve([new vscode.TextEdit(range, text)]);
} catch(e) {
reject(e);
}
});
});
}

}

export = FormattingSupport
}
67 changes: 34 additions & 33 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@ import fs = require('fs');
import path = require('path');
import cp = require('child_process');

import SuggestSupport = require('./goSuggest');
import ExtraInfoSupport = require('./goExtraInfo');
import DeclarationSupport = require('./goDeclaration');
import ReferencesSupport = require('./goReferences');
import FormattingSupport = require('./goFormat');
import RenameSupport = require('./goRename');
import OutlineSupport = require('./goOutline');
import { GoCompletionItemProvider } from './goSuggest';
import { GoHoverProvider } from './goExtraInfo';
import { GoDefinitionProvider } from './goDeclaration';
import { GoReferenceProvider } from './goReferences';
import { GoDocumentFormattingEditProvider } from './goFormat';
import { GoRenameProvider } from './goRename';
import { GoDocumentSybmolProvider } from './goOutline';
import {check, ICheckResult} from './goCheck';
import vscode = require('vscode');

export function activate(subscriptions: vscode.Disposable[]) {
subscriptions.push(vscode.Modes.SuggestSupport.register('go', new SuggestSupport()));
subscriptions.push(vscode.Modes.ExtraInfoSupport.register('go', new ExtraInfoSupport()));
subscriptions.push(vscode.Modes.DeclarationSupport.register('go', new DeclarationSupport()));
subscriptions.push(vscode.Modes.ReferenceSupport.register('go', new ReferencesSupport()));
subscriptions.push(vscode.Modes.FormattingSupport.register('go', new FormattingSupport()));
subscriptions.push(vscode.Modes.RenameSupport.register('go', new RenameSupport()));
subscriptions.push(vscode.Modes.OutlineSupport.register('go', new OutlineSupport()));
let diagnosticCollection: vscode.DiagnosticCollection;

export function activate(subscriptions: vscode.Disposable[]): void {
var GO_MODE = 'go';

subscriptions.push(vscode.languages.registerHoverProvider(GO_MODE, new GoHoverProvider()));
subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider()));
subscriptions.push(vscode.languages.registerDefinitionProvider(GO_MODE, new GoDefinitionProvider()));
subscriptions.push(vscode.languages.registerReferenceProvider(GO_MODE, new GoReferenceProvider()));
subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GO_MODE, new GoDocumentFormattingEditProvider()));
subscriptions.push(vscode.languages.registerDocumentSymbolProvider(GO_MODE, new GoDocumentSybmolProvider()));
subscriptions.push(vscode.languages.registerRenameProvider(GO_MODE, new GoRenameProvider()));

setupGoPathAndOfferToInstallTools();
startBuildOnSaveWatcher();

diagnosticCollection = vscode.languages.createDiagnosticCollection('go');
}

function setupGoPathAndOfferToInstallTools() {
Expand All @@ -39,7 +45,7 @@ function setupGoPathAndOfferToInstallTools() {
if(!process.env["GOPATH"] && gopath) {
process.env["GOPATH"] = gopath;
}

if(!process.env["GOPATH"]) {
vscode.languages.addWarningLanguageStatus("go", "GOPATH not set", () => {
vscode.window.showInformationMessage("GOPATH is not set as an environment variable or via `go.gopath` setting in Code");
Expand Down Expand Up @@ -75,10 +81,10 @@ function setupGoPathAndOfferToInstallTools() {
});

function promptForInstall(missing: string[], status: vscode.Disposable) {
var channel = vscode.window.getOutputChannel('Go');

var channel = vscode.window.createOutputChannel('Go');
channel.reveal();

vscode.window.showInformationMessage("Some Go analysis tools are missing from your GOPATH. Would you like to install them?", {
title: "Install",
command: () => {
Expand All @@ -95,12 +101,8 @@ function setupGoPathAndOfferToInstallTools() {
});
}

let _diagnostics:vscode.Disposable = null;

function deactivate() {
if (_diagnostics) {
_diagnostics.dispose();
}
diagnosticCollection.dispose;
}

function startBuildOnSaveWatcher() {
Expand All @@ -117,26 +119,25 @@ function startBuildOnSaveWatcher() {
vscode.workspace.onDidSaveTextDocument(document => {
var uri = document.getUri();
check(uri.fsPath, config['buildOnSave'], config['lintOnSave'], config['vetOnSave']).then(errors => {
if (_diagnostics) {
_diagnostics.dispose();
}
diagnosticCollection.clear();

var diagnostics = errors.map(error => {
let targetResource = vscode.Uri.file(error.file);
let document = vscode.workspace.getTextDocument(targetResource);
let startColumn = 0;
let endColumn = 1;
if (document) {
let range = new vscode.Range(error.line, 0, error.line, document.getLineMaxColumn(error.line));
let range = new vscode.Range(error.line-1, 0, error.line-1, document.getLine(error.line-1).getEnd().character+1)
let text = document.getTextInRange(range);
let [_, leading, trailing] = /^(\s*).*(\s*)$/.exec(text);
startColumn = leading.length + 1;
endColumn = text.length - trailing.length + 1;
startColumn = leading.length;
endColumn = text.length - trailing.length;
}
let range = new vscode.Range(error.line, startColumn, error.line, endColumn);
let range = new vscode.Range(error.line-1, startColumn, error.line-1, endColumn);
let location = new vscode.Location(uri, range);
return new vscode.Diagnostic(mapSeverityToVSCodeSeverity(error.severity), location, error.msg);
return new vscode.Diagnostic(range, error.msg, mapSeverityToVSCodeSeverity(error.severity));
});
_diagnostics = vscode.languages.addDiagnostics(diagnostics);
diagnosticCollection.set(uri, diagnostics);
}).catch(err => {
vscode.window.showInformationMessage("Error: " + err);
});
Expand Down
70 changes: 38 additions & 32 deletions src/goOutline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,51 @@ interface GoOutlineDeclaration {
children?: GoOutlineDeclaration[];
}

class OutlineSupport implements vscode.Modes.IOutlineSupport {
export class GoDocumentSybmolProvider implements vscode.DocumentSymbolProvider {

private goKindToCodeKind: {[key: string]: string} = {
"package": "module",
"import": "property",
"variable": "variable",
"type": "interface",
"function": "method"
private goKindToCodeKind: { [key: string]: vscode.SymbolKind } = {
"package": vscode.SymbolKind.Package,
"import": vscode.SymbolKind.Namespace,
"variable": vscode.SymbolKind.Variable,
"type": vscode.SymbolKind.Interface,
"function": vscode.SymbolKind.Function
}
public getOutline(document: vscode.TextDocument, token: vscode.CancellationToken): Thenable<vscode.Modes.IOutlineEntry[]> {

public provideDocumentSymbols(document: vscode.TextDocument, token: vscode.CancellationToken): Thenable<vscode.SymbolInformation[]> {

return new Promise((resolve, reject) => {
var filename = document.getUri().fsPath;
var text = document.getText()
var lines = text.split('\n')
var lineLengths = lines.map(line => line.length + 1)

var text = document.getText();
var lines = text.split('\n');
var lineLengths = lines.map(line => line.length + 1);

var toLineCol = (offset: number) => {
for(var i = 0; i < lines.length; i++) {
if(offset < lineLengths[i]) {
return new vscode.Position(i+1, offset)
for (var i = 0; i < lines.length; i++) {
if (offset < lineLengths[i]) {
return new vscode.Position(i, offset)
} else {
offset -= lineLengths[i]
}
}
throw new Error("Illegal offset: " + offset)
}

var convertToCodeSymbols = (decl: GoOutlineDeclaration[], symbols: vscode.SymbolInformation[]): void => {
decl.forEach((each) => {
symbols.push(
new vscode.SymbolInformation(
each.label,
this.goKindToCodeKind[each.type],
new vscode.Range(toLineCol(each.start), toLineCol(each.end - 1))
)
);
if (each.children) {
convertToCodeSymbols(each.children, symbols);
}
})
}

var gooutline = path.join(process.env["GOPATH"], "bin", "go-outline");

// Spawn `go-outline` process
Expand All @@ -57,23 +72,14 @@ class OutlineSupport implements vscode.Modes.IOutlineSupport {
}
if (err) return resolve(null);
var result = stdout.toString();
var decls = <GoOutlineDeclaration[]>JSON.parse(result)
var convert = (decl: GoOutlineDeclaration): vscode.Modes.IOutlineEntry => {
return <vscode.Modes.IOutlineEntry>{
label: decl.label,
type: this.goKindToCodeKind[decl.type],
range: new vscode.Range(toLineCol(decl.start), toLineCol(decl.end-1)),
children: decl.children && decl.children.map(convert)
}
}
var ret = decls.map(convert)
return resolve(ret)
} catch(e) {
var decls = <GoOutlineDeclaration[]>JSON.parse(result);
var symbols: vscode.SymbolInformation[] = [];
convertToCodeSymbols(decls, symbols);
return resolve(symbols)
} catch (e) {
reject(e);
}
});
});
}
}

export = OutlineSupport;
}
Loading

0 comments on commit e9f0bae

Please sign in to comment.