-
Notifications
You must be signed in to change notification settings - Fork 15
Text manipulation within document? #19
Comments
I have written small sample code, which demonstrates, how to use the VSCode API, especially the TextEditor interface of the active editor: For testing, you can create a button. Edit your settings (maybe inside your {
"ego.power-tools": {
"buttons": [
{
"text": "Tests with the editor",
"action": {
"type": "script",
"script": "test_button.js"
}
}
]
}
} Create a exports.execute = async (args) => {
const vscode = args.require('vscode');
// gotoCursor(args, 3, 1);
// gotoEnd(args);
// gotoStart(args);
// getTextAfterCursor(args);
// getTextBeforeCursor(args);
// replaceText(args, 'foo');
// replaceTextAfterCursor(args, 'foo');
// replaceTextBeforeCursor(args, 'foo');
vscode.window.showInformationMessage(
await getClipboardText(args)
);
}
// get text from clipboard
async function getClipboardText(args) {
const vscode = args.require('vscode');
return await vscode.env.clipboard.readText();
}
// find down from cursor
function getTextAfterCursor(args) {
const vscode = args.require('vscode');
const ACTIVE_EDITOR = vscode.window.activeTextEditor;
return ACTIVE_EDITOR.document.getText(new vscode.Range(
ACTIVE_EDITOR.selection.end,
new vscode.Position(ACTIVE_EDITOR.document.lineCount + 1, 0),
));
}
// find up from cursor
function getTextBeforeCursor(args) {
const vscode = args.require('vscode');
const ACTIVE_EDITOR = vscode.window.activeTextEditor;
return ACTIVE_EDITOR.document.getText(new vscode.Range(
new vscode.Position(0, 0),
ACTIVE_EDITOR.selection.start
));
}
// move cursor within file
function gotoCursor(args, line, column) {
const vscode = args.require('vscode');
const ACTIVE_EDITOR = vscode.window.activeTextEditor;
ACTIVE_EDITOR.selection = new vscode.Selection(
line - 1, column - 1,
line - 1, column - 1
);
}
// go to end of file
function gotoEnd(args) {
const vscode = args.require('vscode');
const ACTIVE_EDITOR = vscode.window.activeTextEditor;
ACTIVE_EDITOR.selection = new vscode.Selection(
ACTIVE_EDITOR.document.lineCount + 1, 0,
ACTIVE_EDITOR.document.lineCount + 1, 0
);
}
// go to start of file
function gotoStart(args) {
const vscode = args.require('vscode');
const ACTIVE_EDITOR = vscode.window.activeTextEditor;
ACTIVE_EDITOR.selection = new vscode.Selection(
0, 0,
0, 0
);
}
// replace text of selection / cursor
function replaceText(args, textToPaste) {
const vscode = args.require('vscode');
const ACTIVE_EDITOR = vscode.window.activeTextEditor;
ACTIVE_EDITOR.edit(builder => {
builder.insert(
ACTIVE_EDITOR.selection.active, textToPaste
);
});
}
// replace down from cursor
function replaceTextAfterCursor(args, newText) {
const vscode = args.require('vscode');
const ACTIVE_EDITOR = vscode.window.activeTextEditor;
ACTIVE_EDITOR.edit(builder => {
builder.replace(
new vscode.Range(
ACTIVE_EDITOR.selection.end,
new vscode.Position(ACTIVE_EDITOR.document.lineCount + 1, 0),
),
newText
);
});
}
// replace up from cursor
function replaceTextBeforeCursor(args, newText) {
const vscode = args.require('vscode');
const ACTIVE_EDITOR = vscode.window.activeTextEditor;
ACTIVE_EDITOR.edit(builder => {
builder.replace(
new vscode.Range(
new vscode.Position(0, 0),
ACTIVE_EDITOR.selection.start
),
newText
);
});
} For handling regular expressions, you can use RegExp. |
Thank you - that's an impressive response! |
Thanks a lot! We have added the examples to the repository of samples. If you need more examples, let us know. |
Are the samples linked on the docs? Didn't saw them before. |
I couldn't see any obvious answer to this within the wiki, but can I use this tool to create macros that can manipulate text with the document such as:
The text was updated successfully, but these errors were encountered: