Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-go / lib / utils / modify-tags.js
1 "use strict";
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 === "")
15         ? config.options
16         : 'json=omitempty';
17     const transform = (config.transform || "snakecase");
18     const skipUnexported = config.skipUnexported;
19     let cursor;
20     if (params.prompt) {
21         cursor = await coc_nvim_1.workspace.getCursorPosition();
22         tags = await coc_nvim_1.workspace.requestInput('Enter comma separated tag names', tags);
23         if (!tags) {
24             return;
25         }
26         options = await coc_nvim_1.workspace.requestInput('Enter comma separated options', options);
27     }
28     const args = [
29         '-add-tags', tags.replace(/ +/g, ''),
30         '-override',
31         '-add-options', (options || ""),
32         '-transform', transform,
33         ...(await offsetArgs(document, (params.selection || "struct"), cursor))
34     ];
35     if (skipUnexported) {
36         args.push('--skip-unexported');
37     }
38     await runGomodifytags(document, args);
39 }
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');
46     let cursor;
47     if (params.prompt) {
48         cursor = await coc_nvim_1.workspace.getCursorPosition();
49         tags = await coc_nvim_1.workspace.requestInput('Enter comma separated tag names', tags);
50         if (!tags) {
51             return;
52         }
53     }
54     await runGomodifytags(document, [
55         '-remove-tags', (tags || "json"),
56         '-clear-options',
57         ...(await offsetArgs(document, (params.selection || "struct"), cursor))
58     ]);
59 }
60 exports.removeTags = removeTags;
61 async function clearTags(document, params = {}) {
62     await runGomodifytags(document, [
63         '-clear-tags',
64         '-clear-options',
65         ...(await offsetArgs(document, (params.selection || "struct")))
66     ]);
67 }
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] } });
76 }
77 async function execGomodifytags(args, input) {
78     try {
79         const stdout = await tools_1.execTool(binaries_1.GOMODIFYTAGS, args, input);
80         const mods = JSON.parse(stdout);
81         return {
82             range: {
83                 start: { line: mods.start - 1, character: 0 },
84                 end: { line: mods.end, character: 0 }
85             },
86             newText: mods.lines.join("\n") + "\n"
87         };
88     }
89     catch (err) {
90         coc_nvim_1.workspace.showMessage(`Cannot modify tags: ${err}`, 'error');
91         throw err;
92     }
93 }
94 function fileArchive(fileName, fileContents) {
95     return fileName + '\n' + Buffer.byteLength(fileContents, 'utf8') + '\n' + fileContents;
96 }
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();
102 }
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}`);
106     switch (selection) {
107         case "struct":
108             return ['-offset', byteOffsetAt(document, cursor)];
109         case "line":
110             return ['-line', String(cursor.line + 1)];
111     }
112 }
113 //# sourceMappingURL=modify-tags.js.map