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

Fix varying line number widths #16

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ class Marker extends GutterMarker {
constructor(text: string) {
super();
this.text = text;
this.elementClass = "relative-line-numbers-mono";
}

toDOM() {
return document.createTextNode(this.text);
}
}

function linesCharLength(state: EditorState): number {
/**
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

* Get the character length of the number of lines in the document
* Example: 100 lines -> 3 characters
*/
return state.doc.lines.toString().length;
}

const absoluteLineNumberGutter = gutter({
lineMarker: (view, line) => {
const lineNo = view.state.doc.lineAt(line.from).number;
const absoluteLineNo = new Marker(lineNo.toString());
const charLength = linesCharLength(view.state);
const absoluteLineNo = new Marker(lineNo.toString().padStart(charLength, " "));
const cursorLine = view.state.doc.lineAt(
view.state.selection.asSingle().ranges[0].to
).number;
Expand All @@ -33,15 +43,17 @@ const absoluteLineNumberGutter = gutter({

return null;
},
initialSpacer: () => {
const spacer = new Marker("0");
initialSpacer: (view: EditorView) => {
const spacer = new Marker("0".repeat(linesCharLength(view.state)));
return spacer;
},
});

function relativeLineNumbers(lineNo: number, state: EditorState) {
const charLength = linesCharLength(state);
const blank = " ".padStart(charLength, " ");
if (lineNo > state.doc.lines) {
return " ";
return blank;
}
const cursorLine = state.doc.lineAt(
state.selection.asSingle().ranges[0].to
Expand All @@ -63,9 +75,9 @@ function relativeLineNumbers(lineNo: number, state: EditorState) {
})

if (lineNo === cursorLine) {
return " ";
return blank;
} else {
return (Math.abs(cursorLine - lineNo)-foldedCount).toString();
return (Math.abs(cursorLine - lineNo) - foldedCount).toString().padStart(charLength, " ");
}
}
// This shows the numbers in the gutter
Expand Down
10 changes: 10 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.relative-line-numbers-mono {
font-family: monospace;
white-space: pre;
}

.cm-lineNumbers {
font-family: monospace;
white-space: pre;
min-width: 25px; /* prevent relative line numbers from shifting on files with ~10-20 lines */
}