I want to highlight all whitespace: spaces, tabs, newlines, carriage returns, and form feeds.
So, I’ve tried using highlightSpecialChars like this:
highlightSpecialChars({
render: (code, description, placeholder) => {
console.log(code, description, placeholder);
let rv = document.createElement(‘span’);
rv.innerHTML = placeholder;
return rv;
},
specialChars: /\s/g,
})
but it only matches spaces (and correctly renders dots for spaces).
marijn
September 17, 2024, 3:54pm
2
You may be interested in highlightWhitespace
from the @codemirror /view package.
Tried that first, also did not show newline, just spaces.
marijn
September 17, 2024, 5:08pm
4
Line breaks aren’t rendered as characters in the editor, so you cannot replace them. Are you looking to display a widget at the end of every line (except the last)? You’d have to write a custom extension for that.
Yes, that’s what I’d like.
I have an app which does dynamic testing (compare stdouts) for the students, and every white space matters there.
Are there any examples of similar widgets for me to try and modify?
marijn
September 18, 2024, 7:15am
6
Not directly, but the decoration examples should show all the concepts this would involve.
1 Like
hungify
September 29, 2024, 6:47am
7
@mekterovic
highlightNewLine extension
import type { DecorationSet, EditorView, ViewUpdate } from '@codemirror/view'
import { Decoration, ViewPlugin, WidgetType } from '@codemirror/view'
class NewlineWidget extends WidgetType {
toDOM() {
const span = document.createElement('span')
span.className = 'cm-newline'
span.textContent = '↵'
return span
}
}
function highlightNewLine() {
return ViewPlugin.fromClass(
class {
decorations: DecorationSet
constructor(view: EditorVi…
Check out my comment, it might help you.