Skip to content

Commit

Permalink
fix: editor show error (microsoft#2939)
Browse files Browse the repository at this point in the history
Co-authored-by: Andy Brown <asbrown002@gmail.com>
Co-authored-by: Chris Whitten <christopher.whitten@microsoft.com>
  • Loading branch information
3 people authored May 8, 2020
1 parent ba715ab commit f8087d9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,16 @@ export const TriggerCreationModal: React.FC<TriggerCreationModalProps> = props =
}
};

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 @@ -183,9 +191,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

0 comments on commit f8087d9

Please sign in to comment.