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

Best replacement for vscode.window.activeTextEditor? #933

Closed
TylerLeonhardt opened this issue Jul 1, 2019 · 9 comments
Closed

Best replacement for vscode.window.activeTextEditor? #933

TylerLeonhardt opened this issue Jul 1, 2019 · 9 comments

Comments

@TylerLeonhardt
Copy link

TylerLeonhardt commented Jul 1, 2019

Something that’s done in the PowerShell extension is:

                // v THIS
                const editor = vscode.window.activeTextEditor;
                // ^ THIS
                let selectionRange: vscode.Range;

                if (!editor.selection.isEmpty) {
                    selectionRange =
                        new vscode.Range(
                            editor.selection.start,
                            editor.selection.end);
                } else {
                    selectionRange = editor.document.lineAt(editor.selection.start.line).range;
                }

So that we can get the selected range or the range of the full line if nothing is highlighted. We take this selection and run it in the terminal below.

Is there any good replacement API for this in coc?

@chemzqm
Copy link
Member

chemzqm commented Jul 2, 2019

Use await workspace.document to get current document.
Use workspace.getSelectedRange to get selected range.

@chemzqm chemzqm closed this as completed Jul 2, 2019
@TylerLeonhardt
Copy link
Author

@chemzqm I might be using it wrong… but I get [coc.nvim] Command error: document.getPosition is not a function.

My code:

const doc = await workspace.document;
const range = await workspace.getSelectedRange('v', doc.textDocument);

@chemzqm
Copy link
Member

chemzqm commented Jul 2, 2019

You should pass result of visualmode function as mode argument

@TylerLeonhardt
Copy link
Author

Where can I find that function?

@TylerLeonhardt
Copy link
Author

TylerLeonhardt commented Jul 2, 2019

I found this:

const editMode = (await workspace.nvim.mode).mode

but it doesn’t seem to work right:

const doc = await workspace.document;
const editMode = (await workspace.nvim.mode).mode
const range = await workspace.getSelectedRange(editMode, doc.textDocument);

// OUTPUT: [coc.nvim] Mode 'n' is not supported

This is odd because I was in visual mode when I ran my command that triggers this code (F8 which runs an editor command whos code I’m looking to simplify 😅)

@chemzqm
Copy link
Member

chemzqm commented Jul 2, 2019

@chemzqm
Copy link
Member

chemzqm commented Jul 2, 2019

The command can take argumenst, you can pass visualmode() by :CocAction('runCommand', 'id', visualmode())

@TylerLeonhardt
Copy link
Author

TylerLeonhardt commented Jul 2, 2019

Ok so that gives me this at the bottom:

image

Then I hit ENTER and get:

[coc.nvim] error: TypeError: Cannot read property 'client' of undefined
    at Object.ActionsList.addAction [as execute] (/Users/tyler/.local/share/nvim/plugged/coc.nvim/build/index.js:69304:33)
    at ListManager.doItemAction (/Users/tyler/.local/share/nvim/plugged/coc.nvim/build/index.js:66877:50)
    at process._tickCallback (internal/process/next_tick.js:68:7)

What I ran:

:CocAction("runCommand", "powershell.evaluateSelection", visualmode())

My code:

let doEval = async function(mode: string) {
            let document = await workspace.document
            if (!document || document.filetype !== 'ps1') {
                return
            }
            
            const doc = await workspace.document;
            const range = await workspace.getSelectedRange(mode, doc.textDocument);

            client.sendRequest(EvaluateRequestType, {
                expression: content,
            });

            await proc.scrollToBottom()
        }

let cmdEvalSelection = commands.registerCommand("powershell.evaluateSelection", async (mode) => doEval(mode));

@chemzqm
Copy link
Member

chemzqm commented Jul 3, 2019

Don't know how you invoke action list, it's better to build coc.nvim from source code so you can debug it by Chrome, instead of ask me https://github.com/neoclide/coc.nvim/wiki/Debug-coc.nvim

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