Indent on paste

I have a use case where I want to indent the document once data is pasted?
I have code something like this:


Now I am pasting the following piece of code
on to the selected part(in image 1 it is a line), the output is something like this:

Is there a way to indent the inserted text or worst case scenario the entire doc?

The general approach for this would be to have a transaction filter that detects pastes (tr.isUserEvent("input.paste")) and adds changes that reindent the affected regions (see indentRange in @codemirror/language 6.2.0) for them.

1 Like

indentRange will be available in @codemirror/state 6.2.0 package, right? I had a question when it will be released?

I mistyped @codemirror/state where I meant @codemirror/language.

But yeah, the new change mapping has sat in the repository long enough to release now I guess. Tagged @codemirror/state 6.1.0

Oh ok. Thank you very much!

There is another use case where I am inserting the text in and that text needs to be indented. For the I will get the changeSet from indentRange. I am confused on how to use this changeSet, do I have to dispatch it?

Yes, it goes in the changes field of a transaction spec (view.dispatch({changes: indentRange(...)}))


Thank you very much!