codemirror / dev

Development repository for the CodeMirror editor project
https://codemirror.net/
Other
5.94k stars 376 forks source link

Trying to create a button to change CRLF/LF just like VSCode but showing CR at end of line. #1432

Closed RohitBhati8269 closed 2 months ago

RohitBhati8269 commented 2 months ago

Describe the issue

Trying to create a button to change CRLF/LF just like VSCode but showing CR at the end of the line.

I created a function setLineSeparator which calls when I hit the button to switch LF to CRLF or CRLF to LF. But when I switch from LF to CRLF it changes the lineBreak to \r\n and also updates the lineSeprator value to \r\n but it shows CR at the end of every line(which can be seen in the attached image).

Screenshot 2024-08-29 at 7 36 18 PM

one more issue is that if lineBreaks and lineSeparator are both updated to \r\n but still If I start writing some text and add a new line then that new line comes as '\n' separator why?

Code Snapshot:-

export const eolCompartment = new Compartment();

const state = EditorState.create({
    eolCompartment.of([EditorState.lineSeparator.of('\n')]),
})

function setLineSeparator(eol){
    this.dispatch({
      changes: { from: 0, to: this.state.doc.length, insert: this.getValue().replace(/\r\n|\n|\r/g, eol) },
      effects: eolCompartment.reconfigure(EditorState.lineSeparator.of(eol))
    });
  }

Browser and platform

No response

Reproduction link

No response

marijnh commented 2 months ago

but it shows CR at the end of every line(which can be seen in the attached image).

If the CR is part of the line separator, it doesn't. If you set the line separator to "\n" in a document that has "\r\n" between the lines, yes, you'll see those return characters at the end of your lines.

but still If I start writing some text and add a new line then that new line comes as '\n' separator why?

That's not what I'm seeing when I test this. The document representation (state.doc) does not know about line separators, but if you use state.sliceDoc() or other line-break aware state methods you get your separator between the lines.

RohitBhati8269 commented 2 months ago

Everything working fine besides this:- As I have integrated a lineSeparator in my application, When I Copy a crlf text from outside and paste into my editor. It should change based on my current EOL. Vice-versa. But currently, it is not changing. Anything missing ?

marijnh commented 2 months ago

When I Copy a crlf text from outside and paste into my editor. It should change based on my current EOL.

Did you set up a clipboard input filter?

(Again, you probably don't want to use the lineSeparator option at all, from the way you're describing what you're trying to do, since you don't sound like you actually want rigidly enforced line endings at the document level. You may get better results by removing it, and only inserting the appropriate line separators on output.)

RohitBhati8269 commented 2 months ago

Yes i use clipboardInputFilter, i can fix this issue by just fetching text in clipboardInputFilter function and replace the text but I don't want to use replace() method.

Is there any way to handle this without using replace() method?

Is lineSeparator does not do handle this situation by default?

marijnh commented 2 months ago

Is lineSeparator does not do handle this situation by default?

No it doesn't. It doesn't do what you seem to believe it does, at all. I recommend you stop using it, because it will not help you here.