Skip to content

Commit

Permalink
Better method of targeting newlines in empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
VasylMarchuk committed Dec 26, 2024
1 parent 129ec24 commit d797d7f
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions app/utils/code-mirror-highlight-newlines.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { EditorView, type DecorationSet, type ViewUpdate } from '@codemirror/view';
import { Decoration, ViewPlugin, WidgetType } from '@codemirror/view';
import type { Line } from '@codemirror/state';
import type { DecorationSet, ViewUpdate } from '@codemirror/view';
import { Decoration, EditorView, ViewPlugin, WidgetType } from '@codemirror/view';

class NewlineWidget extends WidgetType {
line: Line;
markerSymbol: string;

constructor({ line, markerSymbol = '↵' }: { line: Line; markerSymbol?: string }) {
super();
this.line = line;
this.markerSymbol = markerSymbol;

Check warning on line 12 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L10-L12

Added lines #L10 - L12 were not covered by tests
}

toDOM() {
const span = document.createElement('span');

Check warning on line 16 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L16

Added line #L16 was not covered by tests

span.textContent = this.markerSymbol;

Check warning on line 18 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L18

Added line #L18 was not covered by tests

span.className = 'cm-newline';

Check warning on line 20 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L20

Added line #L20 was not covered by tests
span.textContent = '↵';

if (this.line.length === 0) {
span.className += ' cm-newline-empty';

Check warning on line 23 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L23

Added line #L23 was not covered by tests
}

return span;

Check warning on line 26 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L26

Added line #L26 was not covered by tests
}
Expand All @@ -16,8 +32,8 @@ const baseTheme = EditorView.baseTheme({
color: 'currentColor',
pointerEvents: 'none',
opacity: '0.5',
'&:not(:only-of-type)': {
paddingLeft: '5px',
'&:not(.cm-newline-empty)': {
paddingLeft: '3px',
},
},
});
Expand All @@ -39,9 +55,9 @@ function highlightNewlines() {
const line = view.state.doc.lineAt(pos);

Check warning on line 55 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L54-L55

Added lines #L54 - L55 were not covered by tests

if (line.length === 0) {
widgets.push(Decoration.widget({ widget: new NewlineWidget(), side: 1 }).range(pos));
widgets.push(Decoration.widget({ widget: new NewlineWidget({ line }), side: 1 }).range(pos));

Check warning on line 58 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L58

Added line #L58 was not covered by tests
} else {
widgets.push(Decoration.widget({ widget: new NewlineWidget(), side: 1 }).range(line.to));
widgets.push(Decoration.widget({ widget: new NewlineWidget({ line }), side: 1 }).range(line.to));

Check warning on line 60 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L60

Added line #L60 was not covered by tests
}

pos = line.to + 1;

Check warning on line 63 in app/utils/code-mirror-highlight-newlines.ts

View check run for this annotation

Codecov / codecov/patch

app/utils/code-mirror-highlight-newlines.ts#L63

Added line #L63 was not covered by tests
Expand Down

0 comments on commit d797d7f

Please sign in to comment.