-
-
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.
Merge branch 'develop' into keyboard-shortcuts
- Loading branch information
Showing
33 changed files
with
187 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
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
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
Oops, something went wrong.