-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
176 lines (159 loc) · 4.99 KB
/
extension.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require("vscode");
const clipboardy = require("clipboardy");
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
let copyPathLines = function (
withLineNumber = false,
withSelection = false,
withPath = false,
noCodeBlock = false
) {
vscode.commands
.executeCommand(
"vscode.executeDocumentSymbolProvider",
vscode.Uri.file(vscode.window.activeTextEditor.document.fileName)
)
.then((symbols) => {});
let alertMessage = "File path not found!";
if (!vscode.workspace.rootPath) {
vscode.window.showWarningMessage(alertMessage);
return false;
}
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage(alertMessage);
return false;
}
let doc = editor.document;
if (doc.isUntitled) {
vscode.window.showWarningMessage(alertMessage);
return false;
}
let path = vscode.workspace.asRelativePath(doc.fileName);
let lineNumbers = [];
let pathRes = "";
let selectionText = "";
if (withLineNumber) {
editor.selections.forEach((selection) => {
if (selection.isSingleLine) {
lineNumbers.push(selection.active.line + 1);
} else {
lineNumbers.push(
selection.start.line + 1 + ":" + (selection.end.line + 1)
);
}
selectionText = editor.document.getText(selection);
});
}
if (withPath && withLineNumber) {
// pathRes = path + ":" + lineNumbers.join(",");
pathRes = ""
}
if (withSelection) {
let language = "";
if (editor.document.languageId) {
language = editor.document.languageId;
if (language === "typescriptreact") {
language = "typescript";
}
}
// Update: 2025-02-28 to use Named Code Blocks
// pathRes += "\n\n"
if (!noCodeBlock) {
// pathRes += "```" + language + "\n"
pathRes += "```" + language + ":" + path + "\n"
}
pathRes += `${selectionText}\n`
if (!noCodeBlock) {
pathRes += "```"
}
}
return pathRes;
};
let toast = function (message) {
vscode.window.setStatusBarMessage(
"`" + message + "` is copied to clipboard",
3000
);
};
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log(
'Congratulations, your extension "copy-relative-path-and-line-numbers" is now active!'
);
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let cmdFileOnlyLineNumber = vscode.commands.registerCommand(
"copy-relative-path-and-line-numbers.justFileLineNumber",
() => {
let message = copyPathLines(true, false, true);
if (message !== false) {
clipboardy.write(message).then(() => {
toast(message);
});
}
}
);
let cmdBoth = vscode.commands.registerCommand(
"copy-relative-path-and-line-numbers.both",
() => {
let message = copyPathLines(true);
if (message !== false) {
clipboardy.write(message).then(() => {
toast(message);
});
}
}
);
let cmdSelectionText = vscode.commands.registerCommand(
"copy-relative-path-and-line-numbers.withText",
() => {
let message = copyPathLines(true, true, true);
if (message !== false) {
clipboardy.write(message).then(() => {
toast(message);
vscode.window.showInformationMessage('Copied to clipboard (snippet+codeblock)');
});
}
}
);
let cmdPathOnly = vscode.commands.registerCommand(
"copy-relative-path-and-line-numbers.path-only",
() => {
let message = copyPathLines();
if (message !== false) {
clipboardy.write(message).then(() => {
toast(message);
vscode.window.showInformationMessage('Copied to clipboard (pathonly)');
});
}
}
);
let cmdNoCodeBlock = vscode.commands.registerCommand(
"copy-relative-path-and-line-numbers.snippet",
() => {
let message = copyPathLines(true, true, true, true);
if (message !== false) {
clipboardy.write(message).then(() => {
toast(message);
vscode.window.showInformationMessage('Copied to clipboard (nocodeblock)');
});
}
}
);
context.subscriptions.push(
cmdFileOnlyLineNumber,
cmdBoth,
cmdPathOnly,
cmdSelectionText,
cmdNoCodeBlock
);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
exports.deactivate = deactivate;