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

Implement relative line numbers #176

Open
sergeichestakov opened this issue May 13, 2024 · 2 comments
Open

Implement relative line numbers #176

sergeichestakov opened this issue May 13, 2024 · 2 comments
Assignees

Comments

@sergeichestakov
Copy link
Contributor

Relative line numbers in Vim has been one of our most requested features for our Vim integration. we could obviously roll our own if we wanted to via another extension but would be great if we could bake this in to the codemirror-vim plugin directly and have it be configurable.

@nightwing how doable and worth it do you think this is?

@nightwing
Copy link
Collaborator

@sergeichestakov this is quite easy to implement

import { lineNumbers } from '@codemirror/view';

function formatNumber() {
  return lineNumbers({
    formatNumber: (lineNo, state) => {
      if (lineNo > state.doc.lines) {
        return lineNo;
      }
      const cursorLine = state.doc.lineAt(state.selection.asSingle().ranges[0].to).number;
      if (lineNo === cursorLine) {
        return lineNo < 10 ? lineNo + "\xB7" : lineNo + "";
      } else {
        return Math.abs(cursorLine - lineNo).toString();
      }
    },
  });
}

We probably can use a compartment to include this in vim extension and make it configurable via set relativenumber

But i am not sure how to make this work for set number, as it is already part of basicSetup

@sergeichestakov
Copy link
Contributor Author

awesome, nice! yeah as for the conflict I think we can try to give it a higher precedence then the line numbers gutter that ships with basicSetup. Otherwise, if that doesn't work, I think it's ok to assume that most users of this extension have more complex use cases that don't involve just using basicSetup and / or are able to filter out the relevant extension if need be. I think wrapping in the right Prec should do the trick though

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