2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.clearTags = exports.removeTags = exports.addTags = void 0;
4 const coc_nvim_1 = require("coc.nvim");
5 const vscode_uri_1 = require("vscode-uri");
6 const tools_1 = require("./tools");
7 const binaries_1 = require("../binaries");
8 ////////////////////////////////////////////////////////////////////////////////
9 async function addTags(document, params = {}) {
10 const config = coc_nvim_1.workspace.getConfiguration().get('go.tags', {});
11 let tags = (params.tags && params.tags.length > 0)
12 ? params.tags.join(',')
13 : (config.tags || 'json');
14 let options = (config.options || config.options === "")
17 const transform = (config.transform || "snakecase");
18 const skipUnexported = config.skipUnexported;
21 cursor = await coc_nvim_1.workspace.getCursorPosition();
22 tags = await coc_nvim_1.workspace.requestInput('Enter comma separated tag names', tags);
26 options = await coc_nvim_1.workspace.requestInput('Enter comma separated options', options);
29 '-add-tags', tags.replace(/ +/g, ''),
31 '-add-options', (options || ""),
32 '-transform', transform,
33 ...(await offsetArgs(document, (params.selection || "struct"), cursor))
36 args.push('--skip-unexported');
38 await runGomodifytags(document, args);
40 exports.addTags = addTags;
41 async function removeTags(document, params = {}) {
42 const config = coc_nvim_1.workspace.getConfiguration().get('go.tags', {});
43 let tags = (params.tags && params.tags.length > 0)
44 ? params.tags.join(',')
45 : (config.tags || 'json');
48 cursor = await coc_nvim_1.workspace.getCursorPosition();
49 tags = await coc_nvim_1.workspace.requestInput('Enter comma separated tag names', tags);
54 await runGomodifytags(document, [
55 '-remove-tags', (tags || "json"),
57 ...(await offsetArgs(document, (params.selection || "struct"), cursor))
60 exports.removeTags = removeTags;
61 async function clearTags(document, params = {}) {
62 await runGomodifytags(document, [
65 ...(await offsetArgs(document, (params.selection || "struct")))
68 exports.clearTags = clearTags;
69 ////////////////////////////////////////////////////////////////////////////////
70 async function runGomodifytags(document, args) {
71 const fileName = vscode_uri_1.URI.parse(document.uri).fsPath;
72 args.push('-modified', '-file', fileName, '-format', 'json');
73 const input = fileArchive(fileName, document.getText());
74 const edit = await execGomodifytags(args, input);
75 await coc_nvim_1.workspace.applyEdit({ changes: { [document.uri]: [edit] } });
77 async function execGomodifytags(args, input) {
79 const stdout = await tools_1.execTool(binaries_1.GOMODIFYTAGS, args, input);
80 const mods = JSON.parse(stdout);
83 start: { line: mods.start - 1, character: 0 },
84 end: { line: mods.end, character: 0 }
86 newText: mods.lines.join("\n") + "\n"
90 coc_nvim_1.workspace.showMessage(`Cannot modify tags: ${err}`, 'error');
94 function fileArchive(fileName, fileContents) {
95 return fileName + '\n' + Buffer.byteLength(fileContents, 'utf8') + '\n' + fileContents;
97 // https://github.com/microsoft/vscode-go/blob/master/src/util.ts#L84
98 function byteOffsetAt(document, position) {
99 const offset = document.offsetAt(position);
100 const text = document.getText();
101 return Buffer.byteLength(text.substr(0, offset)).toString();
103 async function offsetArgs(document, selection, cursor = null) {
104 cursor = cursor || await coc_nvim_1.workspace.getCursorPosition();
105 coc_nvim_1.workspace.showMessage(`selection = ${selection}`);
108 return ['-offset', byteOffsetAt(document, cursor)];
110 return ['-line', String(cursor.line + 1)];
113 //# sourceMappingURL=modify-tags.js.map