-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
91 lines (74 loc) · 3.54 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
'use strict';
const vscode = require('vscode');
function activate(context) {
// Command for deleting lines
let deleteDisposable = vscode.commands.registerCommand('delextension.deleteLines', async () => {
const lineCount = await vscode.window.showInputBox({
prompt: 'Enter the line number to delete',
placeHolder: '1'
});
if (lineCount) {
const editor = vscode.window.activeTextEditor;
if (editor) {
// Get the current line number (0-based index)
const currentLineNumber = editor.selection.start.line;
// Convert the user input to a relative target line number
const relativeLineNumber = parseInt(lineCount);
const targetLineNumber = currentLineNumber + relativeLineNumber;
// Ensure the target line number is within the document's range
const endLine = Math.min(targetLineNumber, editor.document.lineCount - 1);
const preDeletionPosition = editor.selection.active;
// Create a range for deletion
const deleteRange = new vscode.Range(
new vscode.Position(currentLineNumber, 0),
new vscode.Position(endLine, editor.document.lineAt(endLine).text.length)
);
// Perform deletion
editor.edit(editBuilder => {
editBuilder.delete(deleteRange);
}).then(() => {
const newPosition = new vscode.Position(currentLineNumber,preDeletionPosition.character);
vscode.window.activeTextEditor.edit(editBuilder => {
const spaces = ' '.repeat(preDeletionPosition.character);
editBuilder.insert(newPosition, spaces);
});
});
}
}
});
// Command for copying lines based on input
let copyLinesDisposable = vscode.commands.registerCommand('delextension.copyLines', async () => {
const lineCount = await vscode.window.showInputBox({
prompt: 'Enter the relative line number to copy',
placeHolder: '1'
});
if (lineCount) {
const editor = vscode.window.activeTextEditor;
if (editor) {
// Get the current line number (0-based index)
const currentLineNumber = editor.selection.start.line;
// Convert the user input to a relative target line number
const relativeLineNumber = parseInt(lineCount);
const targetLineNumber = currentLineNumber + relativeLineNumber;
// Ensure the target line number is within the document's range
const endLine = Math.min(targetLineNumber, editor.document.lineCount - 1);
// Create a range for copying
const copyRange = new vscode.Range(
new vscode.Position(currentLineNumber, 0),
new vscode.Position(endLine, editor.document.lineAt(endLine).text.length)
);
// Get the selected text for copying
const selectedText = editor.document.getText(copyRange);
// Copy the selected lines to the clipboard
vscode.env.clipboard.writeText(selectedText);
}
}
});
context.subscriptions.push(deleteDisposable);
context.subscriptions.push(copyLinesDisposable);
}
function deactivate() {}
module.exports = {
activate,
deactivate
};