Skip to content

Commit

Permalink
Merge pull request #808 from TypeFox/fix-model-text-content
Browse files Browse the repository at this point in the history
Bugfix: Wrapper; Text model content is not properly updated with updateCodeResource
  • Loading branch information
kaisalmen authored Dec 17, 2024
2 parents 5529ba3 + 35959a2 commit bb4e962
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 95 deletions.
4 changes: 2 additions & 2 deletions packages/examples/src/browser/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const runBrowserEditor = async () => {
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: code,
uri: codeUri
}
Expand Down Expand Up @@ -157,7 +157,7 @@ export const runBrowserEditor = async () => {

await wrapper.start();

wrapper.getTextModels()?.text?.onDidChangeContent(() => {
wrapper.getTextModels()?.modified?.onDidChangeContent(() => {
validate();
});
};
2 changes: 1 addition & 1 deletion packages/examples/src/eclipse.jdt.ls/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const runEclipseJdtLsClient = () => {
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: helloJavaCode,
uri: `${eclipseJdtLsConfig.basePath}/workspace/hello.java`
}
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/src/groovy/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const userConfig: WrapperConfig = {
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: code,
fileExt: 'groovy'
}
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/src/json/client/wrapperWs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const buildJsonClientUserConfig = (htmlContainer?: HTMLElement): WrapperC
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text,
fileExt: 'json'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const setupLangiumClientClassic = async (): Promise<WrapperConfig> => {
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: code,
fileExt: 'langium',
enforceLanguageId: 'langium'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const setupLangiumClientExtended = async (): Promise<WrapperConfig> => {
}],
editorAppConfig: {
codeResources: {
main: {
modified: {
text,
fileExt: 'langium'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export const createLangiumGlobalConfig = async (params: {
extensionFilesOrContents.set(`/${params.languageServerId}-statemachine-configuration.json`, statemachineLanguageConfig);
extensionFilesOrContents.set(`/${params.languageServerId}-statemachine-grammar.json`, responseStatemachineTm);

let main;
let modified;
if (params.text !== undefined) {
main = {
modified = {
text: params.text,
fileExt: 'statemachine'
};
Expand Down Expand Up @@ -97,7 +97,7 @@ export const createLangiumGlobalConfig = async (params: {
}],
editorAppConfig: {
codeResources: {
main
modified
},
monacoWorkerFactory: configureMonacoWorkers
},
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/src/langium/statemachine/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ const startEditor = async () => {

// here the modelReference is created manually and given to the updateEditorModels of the wrapper
const uri = vscode.Uri.parse('/workspace/statemachine-mod.statemachine');
const modelRef = await createModelReference(uri, text);
const modelRefModified = await createModelReference(uri, text);
wrapper.updateEditorModels({
modelRef
modelRefModified
});

// start the second wrapper without any languageclient config
Expand Down
10 changes: 5 additions & 5 deletions packages/examples/src/multi/twoLanguageClients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ print("Hello Moon!")
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: currentText,
fileExt: currenFileExt
}
Expand Down Expand Up @@ -101,9 +101,9 @@ print("Hello Moon!")
}

await wrapper.initAndStart(wrapperConfig);
if (wrapperConfig.editorAppConfig?.codeResources?.main !== undefined) {
(wrapperConfig.editorAppConfig.codeResources.main as CodePlusFileExt).text = currentText;
(wrapperConfig.editorAppConfig.codeResources.main as CodePlusFileExt).fileExt = currenFileExt;
if (wrapperConfig.editorAppConfig?.codeResources?.modified !== undefined) {
(wrapperConfig.editorAppConfig.codeResources.modified as CodePlusFileExt).text = currentText;
(wrapperConfig.editorAppConfig.codeResources.modified as CodePlusFileExt).fileExt = currenFileExt;
}

disableButton('button-flip', false);
Expand All @@ -121,7 +121,7 @@ print("Hello Moon!")
currentText = currentText === textJson ? textPython : textJson;
currenFileExt = currenFileExt === 'json' ? 'py' : 'json';
wrapper.updateCodeResources({
main: {
modified: {
text: currentText,
fileExt: currenFileExt
}
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/src/python/client/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const createUserConfig = (workspaceRoot: string, code: string, codeUri: s
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: code,
uri: codeUri
}
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/src/python/client/reactPython.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const runPythonReact = async () => {
registerFileSystemOverlay(1, fileSystemProvider);

const onTextChanged = (textChanges: TextChanges) => {
console.log(`Dirty? ${textChanges.isDirty}\ntext: ${textChanges.text}\ntextOriginal: ${textChanges.textOriginal}`);
console.log(`Dirty? ${textChanges.isDirty}\ntext: ${textChanges.modified}\ntextOriginal: ${textChanges.original}`);
};
const wrapperConfig = createUserConfig('/workspace', badPyCode, '/workspace/bad.py');
const root = ReactDOM.createRoot(document.getElementById('react-root')!);
Expand Down
8 changes: 4 additions & 4 deletions packages/examples/src/ts/wrapperTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const runTsWrapper = async () => {
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: code,
uri: codeUri
},
Expand Down Expand Up @@ -76,9 +76,9 @@ export const runTsWrapper = async () => {
});
document.querySelector('#button-swap-code')?.addEventListener('click', () => {
const codeResources = wrapper.getMonacoEditorApp()?.getConfig().codeResources;
if ((codeResources?.main as CodePlusUri).uri === codeUri) {
if ((codeResources?.modified as CodePlusUri).uri === codeUri) {
wrapper.updateCodeResources({
main: {
modified: {
text: codeOriginal,
uri: codeOriginalUri
},
Expand All @@ -89,7 +89,7 @@ export const runTsWrapper = async () => {
});
} else {
wrapper.updateCodeResources({
main: {
modified: {
text: code,
uri: codeUri
},
Expand Down
10 changes: 5 additions & 5 deletions packages/wrapper-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ export const MonacoEditorReactComp: React.FC<MonacoEditorProps> = (props) => {
containerRef.current.className = className ?? '';
try {
wrapperRef.current.registerModelUpdate((textModels: TextModels) => {
if (textModels.text !== undefined || textModels.textOriginal !== undefined) {
if (textModels.modified !== undefined || textModels.original !== undefined) {
const newSubscriptions: monaco.IDisposable[] = [];

if (textModels.text !== undefined) {
newSubscriptions.push(textModels.text.onDidChangeContent(() => {
if (textModels.modified !== undefined) {
newSubscriptions.push(textModels.modified.onDidChangeContent(() => {
didModelContentChange(textModels, wrapperConfig.editorAppConfig?.codeResources, onTextChanged);
}));
}

if (textModels.textOriginal !== undefined) {
newSubscriptions.push(textModels.textOriginal.onDidChangeContent(() => {
if (textModels.original !== undefined) {
newSubscriptions.push(textModels.original.onDidChangeContent(() => {
didModelContentChange(textModels, wrapperConfig.editorAppConfig?.codeResources, onTextChanged);
}));
}
Expand Down
18 changes: 10 additions & 8 deletions packages/wrapper-react/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Test MonacoEditorReactComp', () => {
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: 'hello world',
fileExt: 'js'
}
Expand All @@ -61,11 +61,11 @@ describe('Test MonacoEditorReactComp', () => {
};

const textReceiverHello = (textChanges: TextChanges) => {
expect(textChanges.text).toEqual('hello world');
expect(textChanges.modified).toEqual('hello world');
};

const handleOnLoad = async (wrapper: MonacoEditorLanguageClientWrapper) => {
expect(wrapper.getTextModels()?.text?.getValue()).toEqual('hello world');
expect(wrapper.getTextModels()?.modified?.getValue()).toEqual('hello world');
};
render(<MonacoEditorReactComp wrapperConfig={wrapperConfig} onTextChanged={(textReceiverHello)} onLoad={handleOnLoad} />);
});
Expand All @@ -79,7 +79,7 @@ describe('Test MonacoEditorReactComp', () => {
},
editorAppConfig: {
codeResources: {
main: {
modified: {
text: 'hello world',
fileExt: 'js'
}
Expand All @@ -90,21 +90,23 @@ describe('Test MonacoEditorReactComp', () => {

let count = 0;
const textReceiver = (textChanges: TextChanges) => {
// initial call
if (count === 0) {
expect(textChanges.text).toBe('hello world');
expect(textChanges.modified).toBe('hello world');
} else {
expect(textChanges.text).toBe('goodbye world');
expect(textChanges.modified).toBe('goodbye world');
}
};

const handleOnLoad = async (wrapper: MonacoEditorLanguageClientWrapper) => {
count++;
await wrapper.updateCodeResources({
main: {
modified: {
text: 'goodbye world',
fileExt: 'js'
}
});
count++;

};
render(<MonacoEditorReactComp wrapperConfig={wrapperConfig} onLoad={handleOnLoad} onTextChanged={textReceiver} />);
});
Expand Down
Loading

0 comments on commit bb4e962

Please sign in to comment.