Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update some behaviors of showing error message in lu editor #2939

Merged
merged 3 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,16 @@ export const TriggerCreationModal: React.FC<TriggerCreationModalProps> = props =
setFormData({ ...formData, specifiedType: option.key });
};

const getLuDiagnostics = (intent: string, triggerPhrases: string) => {
const content = '#' + intent + '\n' + triggerPhrases;
const { diagnostics } = luIndexer.parse(content);
return combineMessage(diagnostics);
};

const onNameChange = (e, name) => {
setFormData({ ...formData, intent: name });
const errors = formData.errors;
errors.triggerPhrases = getLuDiagnostics(name, formData.triggerPhrases);
setFormData({ ...formData, intent: name, errors });
};

const onChangeRegEx = (e, pattern) => {
Expand All @@ -162,9 +170,7 @@ export const TriggerCreationModal: React.FC<TriggerCreationModalProps> = props =

const onTriggerPhrasesChange = (body: string) => {
const errors = formData.errors;
const content = '#' + formData.intent + '\n' + body;
const { diagnostics } = luIndexer.parse(content);
errors.triggerPhrases = combineMessage(diagnostics);
errors.triggerPhrases = getLuDiagnostics(formData.intent, body);
setFormData({ ...formData, triggerPhrases: body, errors });
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import { LuEditor, EditorDidMount } from '@bfc/code-editor';
import get from 'lodash/get';
import debounce from 'lodash/debounce';
import isEmpty from 'lodash/isEmpty';
import { luIndexer, filterTemplateDiagnostics } from '@bfc/indexers';
import { filterTemplateDiagnostics } from '@bfc/indexers';
import { RouteComponentProps } from '@reach/router';
import querystring from 'query-string';
import { CodeEditorSettings } from '@bfc/shared';

import { StoreContext } from '../../store';
import * as luUtil from '../../utils/luUtil';

const { parse } = luIndexer;

const lspServerPath = '/lu-language-server';

Expand All @@ -28,8 +25,7 @@ const CodeEditor: React.FC<CodeEditorProps> = props => {
const { luFiles, locale, projectId, userSettings } = state;
const { dialogId } = props;
const file = luFiles.find(({ id }) => id === `${dialogId}.${locale}`);
const [diagnostics, setDiagnostics] = useState(get(file, 'diagnostics', []));
const [httpErrorMsg, setHttpErrorMsg] = useState('');
const diagnostics = get(file, 'diagnostics', []);
const [luEditor, setLuEditor] = useState<any>(null);

const search = props.location?.search ?? '';
Expand Down Expand Up @@ -105,37 +101,9 @@ const CodeEditor: React.FC<CodeEditorProps> = props => {
[file, projectId]
);

const updateDiagnostics = useMemo(
() =>
debounce((value: string) => {
if (!file) return;
const { id } = file;
if (inlineMode) {
if (!intent) return;
const { Name } = intent;
const { content } = file;
try {
const newContent = luUtil.updateIntent(content, Name, {
Name,
Body: value,
});
const { diagnostics } = parse(newContent, id);
setDiagnostics(diagnostics);
} catch (error) {
setHttpErrorMsg(error.error);
}
} else {
const { diagnostics } = parse(value, id);
setDiagnostics(diagnostics);
}
}, 1000),
[file, intent, projectId]
);

const _onChange = useCallback(
value => {
setContent(value);
updateDiagnostics(value);
if (!file) return;
if (inlineMode) {
updateLuIntent(value);
Expand All @@ -160,7 +128,6 @@ const CodeEditor: React.FC<CodeEditorProps> = props => {
<LuEditor
editorDidMount={editorDidMount}
value={content}
errorMessage={httpErrorMsg}
diagnostics={currentDiagnostics}
luOption={luOption}
languageServer={{
Expand Down
2 changes: 1 addition & 1 deletion Composer/packages/lib/code-editor/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import formatMessage from 'format-message';
export const inlineModePlaceholder = formatMessage(`> add some example phrases to trigger this intent:
> - please tell me the weather
> - what is the weather like in '{city=Seattle}'
>
> entity definitions:
> @ ml city`);

Expand Down