Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Text manipulation within document? #19

Closed
robertmarkbram opened this issue Apr 2, 2019 · 4 comments
Closed

Text manipulation within document? #19

robertmarkbram opened this issue Apr 2, 2019 · 4 comments
Labels
good first issue Good for newcomers question Further information is requested

Comments

@robertmarkbram
Copy link

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:

  • go to start/end of file
  • move cursor within file
  • regex find/replace up/down from cursor
  • regex find/replace within text in the clipboard or some other buffer (not the file itself)
  • paste text within the file
@ghost
Copy link

ghost commented Apr 2, 2019

@robertmarkbram

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 .vscode subfolder of your workspace) and add an entry:

{
    "ego.power-tools": {
        "buttons": [
            {
                "text": "Tests with the editor",
                "action": {
                    "type": "script",
                    "script": "test_button.js"
                }
            }
        ]
    }
}

Create a test_button.js file inside the same folder.

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.

@ghost ghost added good first issue Good for newcomers question Further information is requested labels Apr 2, 2019
@robertmarkbram
Copy link
Author

Thank you - that's an impressive response!

@ghost
Copy link

ghost commented Apr 2, 2019

Thanks a lot!

We have added the examples to the repository of samples.

If you need more examples, let us know.

@danielo515
Copy link

Are the samples linked on the docs? Didn't saw them before.
Also, I think replacing the selected text is such a common task that you may have a specific api that makes this easier? Like already passing down the selected text as second argument and take whatever the function returns an use it to replace the text? That way the less tech savvy people can benefit from this awesome tool to?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants