Skip to content

Commit

Permalink
Add relative line numbers (issue #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelisDiligens committed Aug 1, 2023
1 parent 741d779 commit 6783a7b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cmoptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

import { PluginSettings } from "./settings";

function showRelativeLines(cm) {
// Source: https://github.com/codemirror/codemirror5/issues/4116
const lineNum = cm.getCursor().line + 1;
if (cm.state.curLineNum === lineNum) {
return;
}
cm.state.curLineNum = lineNum;
cm.setOption('lineNumberFormatter', l =>
l === lineNum ? lineNum : Math.abs(lineNum - l));
}

module.exports = {
default: function (context) {
return {
Expand All @@ -30,6 +41,10 @@ module.exports = {
cm.setOption(key, value);
}
}

// Apply relative line numbers:
if (settings.relativeLineNumbers)
cm.on('cursorActivity', showRelativeLines);
});
},

Expand Down
10 changes: 10 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PluginSettings {
showCursorWhenSelecting: boolean;
cursorBlinkRate: number;
resetSelectionOnContextMenu: boolean;
relativeLineNumbers: boolean;
fixLineNumbersCSS: boolean;
}

Expand All @@ -20,6 +21,7 @@ export async function getPluginSettings(): Promise<PluginSettings> {
"showCursorWhenSelecting": await joplin.settings.value('cmoptions_showCursorWhenSelecting'),
"cursorBlinkRate": await joplin.settings.value('cmoptions_cursorBlinkRate'),
"resetSelectionOnContextMenu": await joplin.settings.value('cmoptions_resetSelectionOnContextMenu'),
"relativeLineNumbers": await joplin.settings.value('cmoptions_relativeLineNumbers'),
"fixLineNumbersCSS": await joplin.settings.value('cmoptions_fixLineNumbersCSS'),
}
}
Expand Down Expand Up @@ -77,6 +79,14 @@ export async function registerAllSettings() {
label: 'Reset selection on context menu',
description: 'Controls whether, when the context menu is opened with a click outside of the current selection, the cursor is moved to the point of the click. Defaults to true.',
},
['cmoptions_relativeLineNumbers']: {
public: true,
section: section,
type: SettingItemType.Bool,
value: false,
label: 'Relative line numbers',
description: 'Enable vim-like relative line numbers',
},
['cmoptions_fixLineNumbersCSS']: {
public: true,
section: section,
Expand Down

0 comments on commit 6783a7b

Please sign in to comment.