Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request - Make it easily extensible by the user #1

Closed
amelio-vazquez-reina opened this issue Dec 15, 2019 · 4 comments
Closed

Comments

@amelio-vazquez-reina
Copy link

This is a very cool extension. I have been looking for something like it here: Using Python or TypeScript to transform text that I select in VSCode.

From what I understand though, the extension currently only supports a fixed, pre-defined set of string transformations or commands. How difficult would it be to make it extensible for the end user ?

For example, let's say that I want to define my own string functions:

function add_space_around_text(selected_text) {
    return "  " + selected_text + "  ";
}

and I'd like to register a command ID of my choice with that function (via settings, i.e. without having to re-install the extension, that is)

IMHO - That could make the extension more extensible and even more powerful.

What do you think?

@brsanthu
Copy link
Collaborator

I had been thinking something like this and also kind of macro that you could create by concatenating list of commands which will be executed in sequence.

Either way, thank you for posting. Will update soon.

@brsanthu
Copy link
Collaborator

brsanthu commented Dec 22, 2019

I have got initial version working. Due to some limitations on the vscode api front, we are not able to do couple of things.

Here is how it works.

Add custom commands to settings.json as below.

...
    "texty": {
        "customCommands": {
            "transformText": {
                "title": "Length of selection",
                "script": "selection.length"
            },
            "addSpaceAroundText": {
                "title": "Add space around text",
                "script": "' ' + selection + ' '"
            }
        }
    }
...

You can add as many commands as possible. When you make changes commands, you need to restart the vscode so we can register those commands again.

Due to api limitation, you cannot search for command in the command palette since as of now we cannot register command dynamically with a title or category of command.

But you can still keybind them using command id. The command id for above custom command will be texty.custom.transformText.

In addition to that, texty also adds additional command Execute Custom Command which shows all defined custom commands as quick pick options (here it shows the title) and user can select one and texty will execute it.

The script can contain any javascript script. You can use following variables in the script.

selection => current selection
moment => moment object import
numeral => moment object import
lodash => lodash import so you can use this to manipulate texts.
_ => lodash import so you can use this to manipulate texts.

Hope this works for you.

brsanthu added a commit that referenced this issue Dec 22, 2019
* #1 Add support for custom script and custom commands

* Fix documentation and context variables
@brsanthu
Copy link
Collaborator

Released as version 1.1.1

@amelio-vazquez-reina
Copy link
Author

This is awesome. Thanks @brsanthu! I'll give this a try soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants