This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
add fillstruct tool #1506
Merged
Merged
add fillstruct tool #1506
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
204d4b5
add fillstruct tool
aeca34c
Refactoring
ramya-rao-a 483d7ab
add tests and remove config
27633c7
Fix tests
ramya-rao-a 45b251e
Use best guess of start line when selection spans multiple lines
ramya-rao-a 1e01158
Remove unused code
ramya-rao-a c771d02
Fix linting errors
ramya-rao-a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,100 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
*--------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import vscode = require('vscode'); | ||
import { byteOffsetAt, getBinPath, getFileArchive, getToolsEnvVars } from './util'; | ||
import cp = require('child_process'); | ||
import { promptForMissingTool } from './goInstallTools'; | ||
import { outputChannel } from './goStatus'; | ||
import { TextEdit } from 'vscode-languageclient/lib/main'; | ||
|
||
// Interface for the output from fillstruct | ||
interface GoFillStructOutput { | ||
start: number; | ||
end: number; | ||
code: string; | ||
} | ||
|
||
export function runFillStruct(editor: vscode.TextEditor) { | ||
let args = getCommonArgs(editor); | ||
if (!args) { | ||
return; | ||
} | ||
|
||
return execFillStruct(editor, args); | ||
} | ||
|
||
function getCommonArgs(editor: vscode.TextEditor): string[] { | ||
if (!editor) { | ||
vscode.window.showInformationMessage('No editor is active.'); | ||
return; | ||
} | ||
if (!editor.document.fileName.endsWith('.go')) { | ||
vscode.window.showInformationMessage('Current file is not a Go file.'); | ||
return; | ||
} | ||
let args = ['-modified', '-file', editor.document.fileName]; | ||
if (editor.selection.isEmpty) { | ||
let offset = byteOffsetAt(editor.document, editor.selection.start); | ||
args.push('-offset'); | ||
args.push(offset.toString()); | ||
} else if (editor.selection.start.line <= editor.selection.end.line) { | ||
args.push('-line'); | ||
args.push(`${editor.selection.start.line + 1},${editor.selection.end.line + 1}`); | ||
} | ||
|
||
return args; | ||
} | ||
|
||
function getTabsCount(editor: vscode.TextEditor): number { | ||
let startline = editor.selection.start.line; | ||
let tabs = editor.document.lineAt(startline).text.match('^\t*'); | ||
return tabs.length; | ||
} | ||
|
||
function execFillStruct(editor: vscode.TextEditor, args: string[]): Promise<void> { | ||
let fillstruct = getBinPath('fillstruct'); | ||
let input = getFileArchive(editor.document); | ||
let tabsCount = getTabsCount(editor); | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
let p = cp.execFile(fillstruct, args, { env: getToolsEnvVars() }, (err, stdout, stderr) => { | ||
try { | ||
if (err && (<any>err).code === 'ENOENT') { | ||
promptForMissingTool('fillstruct'); | ||
return reject(); | ||
} | ||
if (err) { | ||
vscode.window.showInformationMessage(`Cannot fill struct: ${stderr}`); | ||
return reject(); | ||
} | ||
|
||
let output = <GoFillStructOutput[]>JSON.parse(stdout); | ||
|
||
if (output.length === 0) { | ||
vscode.window.showInformationMessage(`Got empty fillstruct output`); | ||
return reject(); | ||
} | ||
|
||
let indent = '\t'.repeat(tabsCount); | ||
let edits: vscode.TextEdit[] = []; | ||
|
||
return editor.edit(editBuilder => { | ||
output.forEach((structToFill) => { | ||
const out = structToFill.code.replace(/\n/g, '\n' + indent); | ||
const rangeToReplace = new vscode.Range(editor.document.positionAt(structToFill.start), | ||
editor.document.positionAt(structToFill.end)); | ||
editBuilder.replace(rangeToReplace, out); | ||
}); | ||
resolve(); | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
p.stdin.end(input); | ||
}); | ||
} |
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 |
---|---|---|
|
@@ -34,6 +34,7 @@ import { isGoPathSet, getBinPath, sendTelemetryEvent, getExtensionCommands, getG | |
import { LanguageClient } from 'vscode-languageclient'; | ||
import { clearCacheForTools } from './goPath'; | ||
import { addTags, removeTags } from './goModifytags'; | ||
import { runFillStruct } from './goFillStruct'; | ||
import { parseLiveFile } from './goLiveErrors'; | ||
import { GoReferencesCodeLensProvider } from './goReferencesCodelens'; | ||
import { implCursor } from './goImpl'; | ||
|
@@ -188,6 +189,10 @@ export function activate(ctx: vscode.ExtensionContext): void { | |
removeTags(args); | ||
})); | ||
|
||
ctx.subscriptions.push(vscode.commands.registerCommand('go.fill.struct', () => { | ||
runFillStruct(vscode.window.activeTextEditor); | ||
})); | ||
|
||
ctx.subscriptions.push(vscode.commands.registerCommand('go.impl.cursor', () => { | ||
implCursor(); | ||
})); | ||
|
@@ -397,6 +402,7 @@ function sendTelemetryEventForConfig(goConfig: vscode.WorkspaceConfiguration) { | |
"includeImports": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }, | ||
"addTags": { "classification": "CustomerContent", "purpose": "FeatureInsight" }, | ||
"removeTags": { "classification": "CustomerContent", "purpose": "FeatureInsight" }, | ||
"fillStruct": { "classification": "CustomerContent", "purpose": "FeatureInsight" }, | ||
"editorContextMenuCommands": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }, | ||
"liveErrors": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }, | ||
"codeLens": { "classification": "SystemMetaData", "purpose": "FeatureInsight" } | ||
|
@@ -434,6 +440,7 @@ function sendTelemetryEventForConfig(goConfig: vscode.WorkspaceConfiguration) { | |
includeImports: goConfig['gotoSymbol'] && goConfig['gotoSymbol']['includeImports'] + '', | ||
addTags: JSON.stringify(goConfig['addTags']), | ||
removeTags: JSON.stringify(goConfig['removeTags']), | ||
fillStruct: JSON.stringify(goConfig['fillStruct']), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only for configuration. Since there is no config called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
editorContextMenuCommands: JSON.stringify(goConfig['editorContextMenuCommands']), | ||
liveErrors: JSON.stringify(goConfig['liveErrors']), | ||
codeLens: JSON.stringify(goConfig['enableCodeLens']) | ||
|
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,19 @@ | ||
package main | ||
|
||
import "time" | ||
|
||
type Struct struct { | ||
String string | ||
Number int | ||
Float float64 | ||
Time time.Time | ||
} | ||
|
||
func main() { | ||
myStruct := Struct{ | ||
String: "", | ||
Number: 0, | ||
Float: 0.0, | ||
Time: time.Time{}, | ||
} | ||
} |
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,14 @@ | ||
package main | ||
|
||
import "time" | ||
|
||
type Struct struct { | ||
String string | ||
Number int | ||
Float float64 | ||
Time time.Time | ||
} | ||
|
||
func main() { | ||
myStruct := Struct{} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the command is run on a selection,
fillstruct
fails withinvalid value "..." for flag -line: strconv.ParseInt: parsing "...": invalid syntax
Looks like the
-line
flag takes a single number and we are passing 2 here.Also, in what scenario do we expect the user select text and then run the command?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had that change sitting on my computer :) Since it takes a
-line
, I thought we might as well support selecting a whole line.