forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goModifytags.ts
157 lines (143 loc) · 4.48 KB
/
goModifytags.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*---------------------------------------------------------
* 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 cp = require('child_process');
import vscode = require('vscode');
import { promptForMissingTool } from './goInstallTools';
import { byteOffsetAt, getBinPath, getFileArchive, getGoConfig, getToolsEnvVars } from './util';
// 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 {
[key: string]: any;
tags: string;
options: string;
promptForTags: boolean;
}
export function addTags(commandArgs: GoTagsConfig) {
const args = getCommonArgs();
if (!args) {
return;
}
getTagsAndOptions(<GoTagsConfig>getGoConfig()['addTags'], commandArgs).then(([tags, options, transformValue]) => {
if (!tags && !options) {
return;
}
if (tags) {
args.push('--add-tags');
args.push(tags);
}
if (options) {
args.push('--add-options');
args.push(options);
}
if (transformValue) {
args.push('--transform');
args.push(transformValue);
}
runGomodifytags(args);
});
}
export function removeTags(commandArgs: GoTagsConfig) {
const args = getCommonArgs();
if (!args) {
return;
}
getTagsAndOptions(<GoTagsConfig>getGoConfig()['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[] {
const 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;
}
const args = ['-modified', '-file', editor.document.fileName, '-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
const 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[]> {
const tags = commandArgs && commandArgs.hasOwnProperty('tags') ? commandArgs['tags'] : config['tags'];
const options = commandArgs && commandArgs.hasOwnProperty('options') ? commandArgs['options'] : config['options'];
const promptForTags =
commandArgs && commandArgs.hasOwnProperty('promptForTags')
? commandArgs['promptForTags']
: config['promptForTags'];
const transformValue: string =
commandArgs && commandArgs.hasOwnProperty('transform') ? commandArgs['transform'] : config['transform'];
if (!promptForTags) {
return Promise.resolve([tags, options, transformValue]);
}
return vscode.window
.showInputBox({
value: tags,
prompt: 'Enter comma separated tag names'
})
.then((inputTags) => {
return vscode.window
.showInputBox({
value: options,
prompt: 'Enter comma separated options'
})
.then((inputOptions) => {
return [inputTags, inputOptions, transformValue];
});
});
}
function runGomodifytags(args: string[]) {
const gomodifytags = getBinPath('gomodifytags');
const editor = vscode.window.activeTextEditor;
const input = getFileArchive(editor.document);
const p = cp.execFile(gomodifytags, args, { env: getToolsEnvVars() }, (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gomodifytags');
return;
}
if (err) {
vscode.window.showInformationMessage(`Cannot modify tags: ${stderr}`);
return;
}
const 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');
});
});
if (p.pid) {
p.stdin.end(input);
}
}