-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Editor): allow relative line numbers in code editors
- Loading branch information
1 parent
72a14e3
commit c88ad99
Showing
11 changed files
with
143 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/livecodes/editor/codemirror/codemirror-line-numbers-relative.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// from https://github.com/jsjoeio/codemirror-line-numbers-relative | ||
|
||
import { EditorView, type ViewUpdate, lineNumbers, gutter, GutterMarker } from '@codemirror/view'; | ||
import { Compartment, type EditorState, type Extension } from '@codemirror/state'; | ||
|
||
const relativeLineNumberGutter = new Compartment(); | ||
|
||
class Marker extends GutterMarker { | ||
/** The text to render in gutter */ | ||
public text: string; | ||
|
||
public constructor(text: string) { | ||
super(); | ||
this.text = text; | ||
} | ||
|
||
public toDOM() { | ||
return document.createTextNode(this.text); | ||
} | ||
} | ||
|
||
const absoluteLineNumberGutter = gutter({ | ||
lineMarker: (view, line) => { | ||
const lineNo = view.state.doc.lineAt(line.from).number; | ||
const absoluteLineNo = new Marker(lineNo.toString()); | ||
const cursorLine = view.state.doc.lineAt(view.state.selection.asSingle().ranges[0].to).number; | ||
|
||
if (lineNo === cursorLine) { | ||
return absoluteLineNo; | ||
} | ||
|
||
return null; | ||
}, | ||
initialSpacer: () => { | ||
const spacer = new Marker('0'); | ||
return spacer; | ||
}, | ||
}); | ||
|
||
function relativeLineNumbers(lineNo: number, state: EditorState) { | ||
if (lineNo > state.doc.lines) { | ||
return ' '; | ||
} | ||
const cursorLine = state.doc.lineAt(state.selection.asSingle().ranges[0].to).number; | ||
if (lineNo === cursorLine) { | ||
return ' '; | ||
} else { | ||
return Math.abs(cursorLine - lineNo).toString(); | ||
} | ||
} | ||
// This shows the numbers in the gutter | ||
const showLineNumbers = relativeLineNumberGutter.of( | ||
lineNumbers({ formatNumber: relativeLineNumbers }), | ||
); | ||
|
||
// This ensures the numbers update | ||
// when selection (cursorActivity) happens | ||
const lineNumbersUpdateListener = EditorView.updateListener.of((viewUpdate: ViewUpdate) => { | ||
if (viewUpdate.selectionSet) { | ||
viewUpdate.view.dispatch({ | ||
effects: relativeLineNumberGutter.reconfigure( | ||
lineNumbers({ formatNumber: relativeLineNumbers }), | ||
), | ||
}); | ||
} | ||
}); | ||
|
||
export function lineNumbersRelative(): Extension { | ||
return [absoluteLineNumberGutter, showLineNumbers, lineNumbersUpdateListener]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters