Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Fixes #922 Pass file contents to go-outline #929

Merged
merged 4 commits into from
Apr 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ install:
- go get -u -v github.com/rogpeppe/godef
- if [[ "$(go version)" =~ "go version go1.5" ]]; then echo hello; else go get -u -v github.com/zmb3/gogetdoc; fi
- if [[ "$(go version)" =~ "go version go1.5" ]]; then echo cannot get golint; else go get -u -v github.com/golang/lint/golint; fi
- go get -u -v github.com/lukehoban/go-outline
- go get -u -v github.com/ramya-rao-a/go-outline
- go get -u -v sourcegraph.com/sqs/goreturns
- go get -u -v golang.org/x/tools/cmd/gorename
- go get -u -v github.com/tpng/gopkgs
Expand Down
4 changes: 4 additions & 0 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ function checkActiveEditor(): vscode.TextEditor {
vscode.window.showInformationMessage('Cannot generate unit tests. File in the editor is not a Go file.');
return;
}
if (editor.document.isDirty) {
vscode.window.showInformationMessage('File has unsaved changes. Save and try again.');
return;
}
return editor;
}

Expand Down
2 changes: 1 addition & 1 deletion src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getTools(goVersion: SemVersion): { [key: string]: string } {
let tools: { [key: string]: string } = {
'gocode': 'github.com/nsf/gocode',
'gopkgs': 'github.com/tpng/gopkgs',
'go-outline': 'github.com/lukehoban/go-outline',
'go-outline': 'github.com/ramya-rao-a/go-outline',
'go-symbols': 'github.com/acroca/go-symbols',
'guru': 'golang.org/x/tools/cmd/guru',
'gorename': 'golang.org/x/tools/cmd/gorename',
Expand Down
5 changes: 2 additions & 3 deletions src/goModifytags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

import vscode = require('vscode');
import { byteOffsetAt, getBinPath } from './util';
import { byteOffsetAt, getBinPath, getFileArchive } from './util';
import cp = require('child_process');
import { promptForMissingTool } from './goInstallTools';

Expand Down Expand Up @@ -125,8 +125,7 @@ function getTagsAndOptions(config: GoTagsConfig, commandArgs: GoTagsConfig): The
function runGomodifytags(args: string[]) {
let gomodifytags = getBinPath('gomodifytags');
let editor = vscode.window.activeTextEditor;
let fileContents = editor.document.getText();
let input = editor.document.fileName + '\n' + Buffer.byteLength(fileContents, 'utf8') + '\n' + fileContents;
let input = getFileArchive(editor.document);
let p = cp.execFile(gomodifytags, args, (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gomodifytags');
Expand Down
35 changes: 29 additions & 6 deletions src/goOutline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import vscode = require('vscode');
import cp = require('child_process');
import path = require('path');
import { getBinPath, sendTelemetryEvent } from './util';
import { getBinPath, getFileArchive } from './util';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';

// Keep in sync with https://github.com/lukehoban/go-outline
// Keep in sync with https://github.com/ramya-rao-a/go-outline
export interface GoOutlineRange {
start: number;
end: number;
Expand All @@ -30,8 +30,20 @@ export interface GoOutlineDeclaration {
}

export interface GoOutlineOptions {
/**
* Path of the file for which outline is needed
*/
fileName: string;

/**
* If true, then the file will be parsed only till imports are collected
*/
importsOnly?: boolean;

/**
* Document to be parsed. If not provided, saved contents of the given fileName is used
*/
document?: vscode.TextDocument;
}

export function documentSymbols(options: GoOutlineOptions): Promise<GoOutlineDeclaration[]> {
Expand All @@ -41,15 +53,24 @@ export function documentSymbols(options: GoOutlineOptions): Promise<GoOutlineDec
if (options.importsOnly) {
gooutlineFlags.push('-imports-only');
}
if (options.document) {
gooutlineFlags.push('-modified');
}
// Spawn `go-outline` process
let p = cp.execFile(gooutline, gooutlineFlags, {}, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('go-outline');
}
if (stderr && stderr.startsWith('flag provided but not defined: -imports-only')) {
if (stderr && stderr.startsWith('flag provided but not defined: ')) {
promptForUpdatingTool('go-outline');
options.importsOnly = false;
if (stderr.startsWith('flag provided but not defined: -imports-only')) {
options.importsOnly = false;
}
if (stderr.startsWith('flag provided but not defined: -modified')) {
options.document = null;
}

return documentSymbols(options).then(results => {
return resolve(results);
});
Expand All @@ -62,6 +83,9 @@ export function documentSymbols(options: GoOutlineOptions): Promise<GoOutlineDec
reject(e);
}
});
if (options.document) {
p.stdin.end(getFileArchive(options.document));
}
});
}

Expand All @@ -78,7 +102,6 @@ export class GoDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
private convertToCodeSymbols(document: vscode.TextDocument, decls: GoOutlineDeclaration[], symbols: vscode.SymbolInformation[], containerName: string): void {
let gotoSymbolConfig = vscode.workspace.getConfiguration('go')['gotoSymbol'];
let includeImports = gotoSymbolConfig ? gotoSymbolConfig['includeImports'] : false;
sendTelemetryEvent('file-symbols', { includeImports: includeImports + '' });
decls.forEach(decl => {
if (!includeImports && decl.type === 'import') return;
let label = decl.label;
Expand All @@ -104,7 +127,7 @@ export class GoDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
}

public provideDocumentSymbols(document: vscode.TextDocument, token: vscode.CancellationToken): Thenable<vscode.SymbolInformation[]> {
let options = { fileName: document.fileName };
let options = { fileName: document.fileName, document: document };
return documentSymbols(options).then(decls => {
let symbols: vscode.SymbolInformation[] = [];
this.convertToCodeSymbols(document, decls, symbols, '');
Expand Down
4 changes: 4 additions & 0 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, args: any)
vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
return;
}
if (editor.document.isDirty) {
vscode.window.showInformationMessage('File has unsaved changes. Save and try again.');
return;
}
getTestFunctions(editor.document).then(testFunctions => {
let testFunction: vscode.SymbolInformation;
// Find any test function containing the cursor.
Expand Down
5 changes: 5 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,8 @@ export function getCurrentGoWorkspaceFromGOPATH(currentFileDirPath: string): str
}
return currentWorkspace;
}

export function getFileArchive(document: vscode.TextDocument): string {
let fileContents = document.getText();
return document.fileName + '\n' + Buffer.byteLength(fileContents, 'utf8') + '\n' + fileContents;
}
2 changes: 1 addition & 1 deletion test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ It returns the number of bytes written and any write error encountered.
// will fail and will have to be replaced with any other go project with vendor packages

let vendorSupportPromise = isVendorSupported();
let filePath = path.join(process.env['GOPATH'], 'src', 'github.com', 'lukehoban', 'go-outline', 'main.go');
let filePath = path.join(process.env['GOPATH'], 'src', 'github.com', 'ramya-rao-a', 'go-outline', 'main.go');
let vendorPkgs = [
'github.com/rogpeppe/godef/vendor/9fans.net/go/acme',
'github.com/rogpeppe/godef/vendor/9fans.net/go/plan9',
Expand Down