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
Use gomodifytags to add/remove tags from struct fields #880
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,140 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import vscode = require('vscode'); | ||
import { byteOffsetAt, getBinPath } from './util'; | ||
import cp = require('child_process'); | ||
import { promptForMissingTool } from './goInstallTools'; | ||
|
||
// Interface for the output from gomodifytags | ||
interface GomodifytagsOutput { | ||
start: number; | ||
end: number; | ||
lines: string[]; | ||
} | ||
|
||
// Interface for settings configuration for adding and removing tags | ||
interface GoTagsConfig { | ||
tags: string; | ||
options: string; | ||
promptForTags: boolean; | ||
} | ||
|
||
export function addTags(commandArgs: GoTagsConfig) { | ||
let args = getCommonArgs(); | ||
if (!args) { | ||
return; | ||
} | ||
|
||
getTagsAndOptions(<GoTagsConfig>vscode.workspace.getConfiguration('go')['addTags'], commandArgs).then(([tags, options]) => { | ||
if (!tags && !options) { | ||
return; | ||
} | ||
if (tags) { | ||
args.push('--add-tags'); | ||
args.push(tags); | ||
} | ||
if (options) { | ||
args.push('--add-options'); | ||
args.push(options); | ||
} | ||
runGomodifytags(args); | ||
}); | ||
|
||
} | ||
|
||
export function removeTags(commandArgs: GoTagsConfig) { | ||
let args = getCommonArgs(); | ||
if (!args) { | ||
return; | ||
} | ||
|
||
getTagsAndOptions(<GoTagsConfig>vscode.workspace.getConfiguration('go')['removeTags'], commandArgs).then(([tags, options]) => { | ||
if (!tags && !options) { | ||
args.push('--clear-tags'); | ||
args.push('--clear-options'); | ||
} | ||
if (tags) { | ||
args.push('--remove-tags'); | ||
args.push(tags); | ||
} | ||
if (options) { | ||
args.push('--remove-options'); | ||
args.push(options); | ||
} | ||
runGomodifytags(args); | ||
}); | ||
} | ||
|
||
function getCommonArgs(): string[] { | ||
let editor = vscode.window.activeTextEditor; | ||
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', '-format', 'json']; | ||
if (editor.selection.start.line === editor.selection.end.line && editor.selection.start.character === editor.selection.end.character) { | ||
// Add tags to the whole struct | ||
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) { | ||
// Add tags to selected lines | ||
args.push('-line'); | ||
args.push(`${editor.selection.start.line + 1},${editor.selection.end.line + 1}`); | ||
} | ||
|
||
return args; | ||
} | ||
|
||
function getTagsAndOptions(config: GoTagsConfig, commandArgs: GoTagsConfig): Thenable<string[]> { | ||
let tags = commandArgs && commandArgs.hasOwnProperty('tags') ? commandArgs['tags'] : config['tags']; | ||
let options = commandArgs && commandArgs.hasOwnProperty('options') ? commandArgs['options'] : config['options']; | ||
let promptForTags = commandArgs && commandArgs.hasOwnProperty('promptForTags') ? commandArgs['promptForTags'] : config['promptForTags']; | ||
|
||
if (!promptForTags) { | ||
return Promise.resolve([tags, options]); | ||
} | ||
|
||
return vscode.window.showInputBox({ | ||
value: 'json', | ||
prompt: 'Enter comma separated tag names' | ||
}).then(inputTags => { | ||
return vscode.window.showInputBox({ | ||
value: 'json=omitempty,xml=cdata', | ||
prompt: 'Enter comma separated options' | ||
}).then(inputOptions => { | ||
return [inputTags, inputOptions]; | ||
}); | ||
}); | ||
} | ||
|
||
function runGomodifytags(args: string[]) { | ||
let gomodifytags = getBinPath('gomodifytags'); | ||
let editor = vscode.window.activeTextEditor; | ||
let fileContents = editor.document.getText(); | ||
let input = editor.document.fileName + '\n' + fileContents.length + '\n' + fileContents; | ||
let p = cp.execFile(gomodifytags, args, (err, stdout, stderr) => { | ||
if (err && (<any>err).code === 'ENOENT') { | ||
promptForMissingTool('gomodifytags'); | ||
return; | ||
} | ||
if (err) { | ||
vscode.window.showInformationMessage(`Cannot modify tags: ${stderr}`); | ||
return; | ||
} | ||
let output = <GomodifytagsOutput>JSON.parse(stdout); | ||
vscode.window.activeTextEditor.edit(editBuilder => { | ||
editBuilder.replace(new vscode.Range(output.start - 1, 0, output.end, 0), output.lines.join('\n') + '\n'); | ||
}); | ||
}); | ||
p.stdin.end(input); | ||
} |
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.
Can you detect whether the tool is installed earlier, before the user has entered tag names and everything?
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.
We could, but this is the model we follow for all other tools, so will not be changing this unless we change the rest of the places