Skip to content

Commit

Permalink
src/goModifytags: support gomodifytags's --template flag
Browse files Browse the repository at this point in the history
Support gomodifytags 1.11.0 new --template feature

As of [1.11.0](https://github.com/fatih/gomodifytags/releases/tag/v1.11.0) there is a `--template` option in gomofidytags that allows to give a special formatting to the tags, this adds support to it.

Change-Id: I94949bbfa87941a4b04b5671e728442a849e0e5e
GitHub-Last-Rev: b26a2a6
GitHub-Pull-Request: golang#826
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/264299
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
Reviewed-by: Suzy Mueller <[email protected]>
Trust: Suzy Mueller <[email protected]>
Trust: Hyang-Ah Hana Kim <[email protected]>
  • Loading branch information
perrito666 authored and suzmue committed Nov 2, 2020
1 parent d67c057 commit 99c4f74
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
},
"editor.insertSpaces": false,
"typescript.tsdk": "node_modules\\typescript\\lib",
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,8 @@
"tags": "json",
"options": "json=omitempty",
"promptForTags": false,
"transform": "snakecase"
"transform": "snakecase",
"template": ""
},
"description": "Tags and options configured here will be used by the Add Tags command to add tags to struct fields. If promptForTags is true, then user will be prompted for tags and options. By default, json tags are added.",
"scope": "resource"
Expand Down
29 changes: 25 additions & 4 deletions src/goModifytags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import cp = require('child_process');
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool } from './goInstallTools';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { byteOffsetAt, getBinPath, getFileArchive, getGoConfig } from './util';

// Interface for the output from gomodifytags
Expand All @@ -24,6 +24,7 @@ interface GoTagsConfig {
tags: string;
options: string;
promptForTags: boolean;
template: string;
}

export function addTags(commandArgs: GoTagsConfig) {
Expand All @@ -32,7 +33,8 @@ export function addTags(commandArgs: GoTagsConfig) {
return;
}

getTagsAndOptions(<GoTagsConfig>getGoConfig()['addTags'], commandArgs).then(([tags, options, transformValue]) => {
getTagsAndOptions(<GoTagsConfig>getGoConfig()['addTags'], commandArgs).then((
[tags, options, transformValue, template]) => {
if (!tags && !options) {
return;
}
Expand All @@ -48,6 +50,10 @@ export function addTags(commandArgs: GoTagsConfig) {
args.push('--transform');
args.push(transformValue);
}
if (template) {
args.push('--template');
args.push(template);
}
runGomodifytags(args);
});
}
Expand Down Expand Up @@ -112,9 +118,11 @@ function getTagsAndOptions(config: GoTagsConfig, commandArgs: GoTagsConfig): The
: config['promptForTags'];
const transformValue: string =
commandArgs && commandArgs.hasOwnProperty('transform') ? commandArgs['transform'] : config['transform'];
const format: string =
commandArgs && commandArgs.hasOwnProperty('template') ? commandArgs['template'] : config['template'];

if (!promptForTags) {
return Promise.resolve([tags, options, transformValue]);
return Promise.resolve([tags, options, transformValue, format]);
}

return vscode.window
Expand All @@ -135,7 +143,14 @@ function getTagsAndOptions(config: GoTagsConfig, commandArgs: GoTagsConfig): The
prompt: 'Enter transform value'
})
.then((transformOption) => {
return [inputTags, inputOptions, transformOption];
return vscode.window
.showInputBox({
value: format,
prompt: 'Enter template value'
})
.then((template) => {
return [inputTags, inputOptions, transformOption, template];
});
});
});
});
Expand All @@ -150,6 +165,12 @@ function runGomodifytags(args: string[]) {
promptForMissingTool('gomodifytags');
return;
}
if (err && (<any>err).code === 2 && args.indexOf('--template') > 0) {
vscode.window.showInformationMessage(`Cannot modify tags: you might be using a` +
`version that does not support --template`);
promptForUpdatingTool('gomodifytags');
return;
}
if (err) {
vscode.window.showInformationMessage(`Cannot modify tags: ${stderr}`);
return;
Expand Down

0 comments on commit 99c4f74

Please sign in to comment.