Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Composer/packages/lib/code-editor/src/BaseEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const defaultOptions = {
glyphMargin: false,
folding: false,
renderLineHighlight: 'none',
formatOnType: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure this doesn't cause weirdness in the other places that the code-editor is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure this doesn't cause weirdness in the other places that the code-editor is used.

I tested the editors in composer. Only the LG editor will have the ability to format structure lg input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for investigating @cosmicshuai

};

const styles = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import {
CompletionItem,
Range,
DiagnosticSeverity,
TextEdit,
} from 'vscode-languageserver-types';
import { TextDocumentPositionParams } from 'vscode-languageserver-protocol';
import { TextDocumentPositionParams, DocumentOnTypeFormattingParams } from 'vscode-languageserver-protocol';
import get from 'lodash/get';
import { filterTemplateDiagnostics, isValid } from '@bfc/indexers';
import { MemoryResolver } from '@bfc/shared';
Expand Down Expand Up @@ -84,11 +85,15 @@ export class LGServer {
},
hoverProvider: true,
foldingRangeProvider: false,
documentOnTypeFormattingProvider: {
firstTriggerCharacter: '\n',
},
},
};
});
this.connection.onCompletion(params => this.completion(params));
this.connection.onHover(params => this.hover(params));
this.connection.onDocumentOnTypeFormatting(docTypingParams => this.docTypeFormat(docTypingParams));

this.connection.onRequest((method, params) => {
if (InitializeDocumentsMethodName === method) {
Expand Down Expand Up @@ -306,7 +311,10 @@ export class LGServer {
return resultArr.join(' ,');
}

private matchLineState(params: TextDocumentPositionParams): LGCursorState | undefined {
private matchLineState(
params: TextDocumentPositionParams,
templateId: string | undefined
): LGCursorState | undefined {
const state: LGCursorState[] = [];
const document = this.documents.get(params.textDocument.uri);
if (!document) return;
Expand All @@ -318,15 +326,18 @@ export class LGServer {
state.push(TEMPLATENAME);
} else if (line.trim().startsWith('-')) {
state.push(TEMPLATEBODY);
} else if (state[state.length - 1] === TEMPLATENAME && (line.trim() === '[' || line.trim() === '[')) {
} else if (
(state[state.length - 1] === TEMPLATENAME || templateId) &&
(line.trim() === '[' || line.trim() === '[]')
) {
state.push(STRUCTURELG);
}
}

return state.length >= 1 ? state.pop() : undefined;
}

private matchCardTypeState(params: TextDocumentPositionParams): string | undefined {
private matchCardTypeState(params: TextDocumentPositionParams, templateId: string | undefined): string | undefined {
const state: LGCursorState[] = [];
const document = this.documents.get(params.textDocument.uri);
if (!document) return;
Expand All @@ -339,7 +350,7 @@ export class LGServer {
state.push(TEMPLATENAME);
} else if (line.trim().startsWith('-')) {
state.push(TEMPLATEBODY);
} else if (state[state.length - 1] === TEMPLATENAME && line.trim().startsWith('[')) {
} else if ((state[state.length - 1] === TEMPLATENAME || templateId) && line.trim().startsWith('[')) {
state.push(STRUCTURELG);
lastLine = line;
} else if (state[state.length - 1] === STRUCTURELG && line.trim() === '') {
Expand Down Expand Up @@ -490,7 +501,9 @@ export class LGServer {
const range = getRangeAtPosition(document, position);
const wordAtCurRange = document.getText(range);
const endWithDot = wordAtCurRange.endsWith('.');
const lgFile = this.getLGDocument(document)?.index();
const lgDoc = this.getLGDocument(document);
const lgFile = lgDoc?.index();
const templateId = lgDoc?.templateId;
if (!lgFile) {
return Promise.resolve(null);
}
Expand Down Expand Up @@ -520,7 +533,7 @@ export class LGServer {

const completionPropertyResult = this.findValidMemoryVariables(params);

const curLineState = this.matchLineState(params);
const curLineState = this.matchLineState(params, templateId);

if (curLineState === STRUCTURELG) {
const cardTypesSuggestions: CompletionItem[] = cardTypes.map(type => {
Expand All @@ -538,7 +551,7 @@ export class LGServer {
});
}

const cardType = this.matchCardTypeState(params);
const cardType = this.matchCardTypeState(params, templateId);
if (cardType && cardTypes.includes(cardType)) {
let item: CompletionItem | undefined = undefined;
if (cardType === 'CardAction') {
Expand Down Expand Up @@ -614,6 +627,50 @@ export class LGServer {
}
}

protected async docTypeFormat(params: DocumentOnTypeFormattingParams): Promise<TextEdit[] | null> {
const document = this.documents.get(params.textDocument.uri);
if (!document) {
return Promise.resolve(null);
}

const edits: TextEdit[] = [];
const key = params.ch;
const position = params.position;
const range = Range.create(0, 0, position.line, position.character);
const lines = document.getText(range).split('\n');
const isInStructureLGMode = this.matchStructureLG(lines);
if (key === '\n' && isInStructureLGMode) {
const deleteRange = Range.create(position.line, 0, position.line, 4);
const deleteItem: TextEdit = TextEdit.del(deleteRange);
if (document.getText(deleteRange).trim() === '') {
edits.push(deleteItem);
}
}

return Promise.resolve(edits);
}

private matchStructureLG(lines: string[]): boolean {
if (lines.length === 0) return false;
let state = ROOT;
const propertyDefinitionRegex = /[^=]+=.*/;
for (const line of lines) {
if (state === ROOT && line.trim().startsWith('[')) {
state = STRUCTURELG;
} else if ((state === STRUCTURELG && propertyDefinitionRegex.test(line)) || line.trim() === '') {
continue;
} else {
state = ROOT;
}
}

if (state === ROOT) {
return false;
}

return true;
}

protected validate(document: TextDocument): void {
this.cleanPendingValidation(document);
this.pendingValidationRequests.set(
Expand Down