Skip to content

Commit

Permalink
fix multilanguage go to incorrect behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicshuai committed Mar 24, 2021
1 parent 4bc5587 commit 86d30b4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
10 changes: 9 additions & 1 deletion Composer/packages/lib/code-editor/src/lg/LgCodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export const LgCodeEditor = (props: LgCodeEditorProps) => {
const [editor, setEditor] = useState<any>();

useEffect(() => {
if (props.options?.readOnly) {
return;
}

if (!editor) return;

if (!window.monacoServiceInstance) {
Expand All @@ -127,6 +131,7 @@ export const LgCodeEditor = (props: LgCodeEditorProps) => {
['botbuilderlg'],
connection
);

sendRequestWithRetry(languageClient, 'initializeDocuments', { lgOption, uri });
const disposable = languageClient.start();
connection.onClose(() => disposable.dispose());
Expand All @@ -135,14 +140,17 @@ export const LgCodeEditor = (props: LgCodeEditorProps) => {
languageClient.onReady().then(() =>
languageClient.onNotification('GotoDefinition', (result) => {
if (lgOption?.projectId) {
console.log('LG option project id:', lgOption?.projectId);
onNavigateToLgPage?.(result.fileId, { templateId: result.templateId, line: result.line });
}
})
);
},
});
} else {
sendRequestWithRetry(window.monacoLGEditorInstance, 'initializeDocuments', { lgOption, uri });
if (!props.options?.readOnly) {
sendRequestWithRetry(window.monacoLGEditorInstance, 'initializeDocuments', { lgOption, uri });
}
window.monacoLGEditorInstance.onReady().then(() =>
window.monacoLGEditorInstance.onNotification('GotoDefinition', (result) => {
if (lgOption?.projectId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class LGServer {
const textDocument = this.documents.get(uri);
if (textDocument) {
this.addLGDocument(textDocument, lgOption);
this.recordTemplatesDefintions(textDocument, lgOption);
this.recordTemplatesDefintions(lgOption);
this.validateLgOption(textDocument, lgOption);
this.validate(textDocument);
this.getOtherLGVariables(lgOption);
Expand Down Expand Up @@ -313,24 +313,55 @@ export class LGServer {
this.LGDocuments.push(lgDocument);
}

protected async recordTemplatesDefintions(document: TextDocument, lgOption?: LGOption) {
protected async recordTemplatesDefintions(lgOption?: LGOption) {
const { fileId, projectId } = lgOption || {};
const lgTextFiles = projectId ? this.getLgResources(projectId) : [];
for (const file of lgTextFiles) {
//Only stroe templates in other LG files
if (file.id !== fileId) {
const lgTemplates = await this._lgParser.parse(file.id, file.content, lgTextFiles);
for (const template of lgTemplates.templates) {
this._templateDefinitions[template.name] = {
fileId: file.id,
templateId: template.name,
line: template?.range?.start?.line,
};
if (projectId) {
const curLocale = this.getLocale(fileId);
const fileIdWitoutLocale = this.removeLocaleInId(fileId);
const lgTextFiles = projectId ? this.getLgResources(projectId) : [];
for (const file of lgTextFiles) {
//Only stroe templates in other LG files
if (this.removeLocaleInId(file.id) !== fileIdWitoutLocale && this.getLocale(file.id) === curLocale) {
const lgTemplates = await this._lgParser.parse(file.id, file.content, lgTextFiles);
this._templateDefinitions = {};
for (const template of lgTemplates.templates) {
this._templateDefinitions[template.name] = {
fileId: file.id,
templateId: template.name,
line: template?.range?.start?.line,
};
}
}
}
}
}

private removeLocaleInId(fileId: string | undefined): string {
if (!fileId) {
return '';
}

const idx = fileId.lastIndexOf('.');
if (idx !== -1) {
return fileId.substring(0, idx);
} else {
return fileId;
}
}

private getLocale(fileId: string | undefined): string {
if (!fileId) {
return '';
}

const idx = fileId.lastIndexOf('.');
if (idx !== -1) {
return fileId.substring(idx, fileId.length);
} else {
return '';
}
}

protected getLGDocument(document: TextDocument): LGDocument | undefined {
return this.LGDocuments.find(({ uri }) => uri === document.uri);
}
Expand Down

0 comments on commit 86d30b4

Please sign in to comment.