Update model uri without changing model #3751
Replies: 8 comments
-
Currently, this is not possible. You could, however ... use some creative JS to get this to work. // Assuming model1 is the previous model
var model2 = monaco.editor.createModel(model1.getValue(), undefined, newURI);
var cm2 = model2._commandManager;
var cm1 = model1._commandManager;
var temp;
// SWAP currentOpenStackElement
temp = cm2.currentOpenStackElement;
cm2.currentOpenStackElement = cm1.currentOpenStackElement;
cm1.currentOpenStackElement = tmp;
// SWAP past
temp = cm2.past;
cm2.past = cm1.past;
cm1.past = tmp;
// SWAP future
temp = cm2.future;
cm2.future = cm1.future;
cm1.future = tmp; |
Beta Was this translation helpful? Give feedback.
-
Thank you! |
Beta Was this translation helpful? Give feedback.
-
This looks a little bit like playing with 🔥 in internals. Is there any more 'official' API that could be introduced?
|
Beta Was this translation helpful? Give feedback.
-
I can't run your code @alexandrudima , I always have the following error :
So i tried something else like :
and got the same error message... I can't figure out what I'm doing wrong |
Beta Was this translation helpful? Give feedback.
-
I could use a |
Beta Was this translation helpful? Give feedback.
-
To copy the undostack first get it:
literally copy it to the new model:
note that EditStackElement is an internal class to monaco, not sure if you can easily export it but redefining it works fine because it doesn't have any external references
important |
Beta Was this translation helpful? Give feedback.
-
for anyone who comes from new version (0.26.1), here is a workaround function switchModelToNewUri(oldModel, newUri) {
const newModel = monaco.editor.createModel(
oldModel.getValue(),
oldModel.getLanguageIdentifier().language,
newUri,
)
const fsPath = newUri.fsPath // \\filename
const formatted = newUri.toString() // file:///filename
const editStacks = oldModel._commandManager._undoRedoService._editStacks
const newEditStacks = new Map()
function adjustEditStack(c) {
c.actual.model = newModel
c.resourceLabel = fsPath
c.resourceLabels = [fsPath]
c.strResource = formatted
c.strResources = [formatted]
}
editStacks.forEach((s) => {
s.resourceLabel = fsPath
s.strResource = formatted
s._future.forEach(adjustEditStack)
s._past.forEach(adjustEditStack)
newEditStacks.set(formatted, s)
})
newModel._commandManager._undoRedoService._editStacks = newEditStacks
oldModel.dispose()
return newModel
} |
Beta Was this translation helpful? Give feedback.
-
A Would be useful when the file is being renamed to keep language features that depend on |
Beta Was this translation helpful? Give feedback.
-
Is there a way I can update the model's URI without creating a new one?
If not, is there a way to copy the undo stack into the new model?
Beta Was this translation helpful? Give feedback.
All reactions