Skip to content

Extension API: cursor position (determining current location and moving it) #111

Closed

Description

@jpoon

Seems like the only way to get the current cursor position is if the user explicitly selects something.

Activity

  1. alefragnani commented on Nov 19, 2015

    @alefragnani

    Hi Jason Poon (@jpoon),

    Even if there is no selected text, you have to use the selection object. Something like this:

        // current editor
        const editor = vscode.window.activeTextEditor;
    
        // check if there is no selection
        if (editor.selection.isEmpty) {
          // the Position object gives you the line and character where the cursor is
          const position = editor.selection.active;
        }
    

    Hope this helps

  2. jpoon commented on Nov 19, 2015

    @jpoon
    Author

    Alessandro Fragnani (@alefragnani) perfect. I'm going to close this issue 👍

  3. jpoon commented on Nov 19, 2015

    @jpoon
    Author

    Alessandro Fragnani (@alefragnani) one more question, is there a way to move the cursor?

    Say for the sample code you posted:

        // current editor
        const editor = vscode.window.activeTextEditor;
    
        // check if there is no selection
        if (editor.selection.isEmpty) {
          // the Position object gives you the line and character where the cursor is
          const position = editor.selection.active;
    
          // move cursor to this new position?
          var newPosition = position.with(position.line, 0);
        }
    
  4. changed the title [-]Extension API: Determine current cursor position[/-] [+]Extension API: cursor position (determining current location and moving it)[/+] on Nov 19, 2015
  5. joaomoreno commented on Nov 19, 2015

    @joaomoreno
    Contributor
  6. joaomoreno commented on Nov 19, 2015

    @joaomoreno
    Contributor

    Johannes Rieken (@jrieken) The API should probably indicate in the doc comments what can be set.

  7. jpoon commented on Nov 19, 2015

    @jpoon
    Author

    😍 Perfect!

            const editor = vscode.window.activeTextEditor;
            const position = editor.selection.active;
    
            var newPosition = position.with(position.line, 0);
            var newSelection = new vscode.Selection(newPosition, newPosition);
            editor.selection = newSelection;
    

    +1 on the docs to denote setters.

  8. jrieken commented on Nov 19, 2015

    @jrieken
    Contributor

    All properties that don't have the @readonly tag can be set.

  9. locked and limited conversation to collaborators on Nov 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions