From a5700fa3f7888ef74df207bdf92306a5b0d27386 Mon Sep 17 00:00:00 2001 From: Ramya Rao Date: Mon, 17 Apr 2017 15:54:07 -0700 Subject: [PATCH] Fixes #922 Pass file contents to go-outline (#929) * Fixes #922 Pass file contents to go-outline * Use fork of go-outline * Fixing broken test * If test files are dirty, dont run tests --- .travis.yml | 2 +- src/goGenerateTests.ts | 4 ++++ src/goInstallTools.ts | 2 +- src/goModifytags.ts | 5 ++--- src/goOutline.ts | 35 +++++++++++++++++++++++++++++------ src/goTest.ts | 4 ++++ src/util.ts | 5 +++++ test/go.test.ts | 2 +- 8 files changed, 47 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5cf2521c..d7871a97 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/goGenerateTests.ts b/src/goGenerateTests.ts index 7962a1ef..6e3c2a74 100644 --- a/src/goGenerateTests.ts +++ b/src/goGenerateTests.ts @@ -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; } diff --git a/src/goInstallTools.ts b/src/goInstallTools.ts index e00a913f..08755db5 100644 --- a/src/goInstallTools.ts +++ b/src/goInstallTools.ts @@ -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', diff --git a/src/goModifytags.ts b/src/goModifytags.ts index e36c93c7..00f58b16 100644 --- a/src/goModifytags.ts +++ b/src/goModifytags.ts @@ -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'; @@ -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 && (err).code === 'ENOENT') { promptForMissingTool('gomodifytags'); diff --git a/src/goOutline.ts b/src/goOutline.ts index 8c80aee4..ce34532c 100644 --- a/src/goOutline.ts +++ b/src/goOutline.ts @@ -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; @@ -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 { @@ -41,15 +53,24 @@ export function documentSymbols(options: GoOutlineOptions): Promise { try { if (err && (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); }); @@ -62,6 +83,9 @@ export function documentSymbols(options: GoOutlineOptions): Promise { if (!includeImports && decl.type === 'import') return; let label = decl.label; @@ -104,7 +127,7 @@ export class GoDocumentSymbolProvider implements vscode.DocumentSymbolProvider { } public provideDocumentSymbols(document: vscode.TextDocument, token: vscode.CancellationToken): Thenable { - 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, ''); diff --git a/src/goTest.ts b/src/goTest.ts index 9c5d108c..631eb24a 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -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. diff --git a/src/util.ts b/src/util.ts index f77472a6..0e863f5a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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; +} \ No newline at end of file diff --git a/test/go.test.ts b/test/go.test.ts index a3e70598..33e4b0bc 100644 --- a/test/go.test.ts +++ b/test/go.test.ts @@ -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',