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

feat: LG LSP in Composer #1504

Merged
merged 162 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
162 commits
Select commit Hold shift + click to select a range
a3be983
Add LSP of LG
cosmicshuai Sep 26, 2019
967c133
change folder name
cosmicshuai Sep 26, 2019
50eead9
change folder location
cosmicshuai Sep 26, 2019
9e4da28
seperate server and client demo
cosmicshuai Sep 26, 2019
1963f3a
add completion and hover for builtin-functions
cosmicshuai Sep 26, 2019
4ed777e
change file names
cosmicshuai Sep 26, 2019
ba5b6d4
change dependency
cosmicshuai Sep 27, 2019
c58ae8c
refactor the package
cosmicshuai Sep 27, 2019
85a2523
update demo readme
cosmicshuai Sep 27, 2019
415a11c
merge changes from master
cosmicshuai Sep 29, 2019
934211d
make some refine
Danieladu Oct 3, 2019
e825a94
Merge branch 'master' into shuwan/AddingLG_LSP
Danieladu Oct 3, 2019
6699260
remove npm lock file
Danieladu Oct 3, 2019
2a3957b
Merge branch 'master' into shuwan/AddingLG_LSP
cosmicshuai Oct 9, 2019
f4dd101
add demos to workspace and run test in 1 command
cosmicshuai Oct 9, 2019
4e34574
remove declaration files
cosmicshuai Oct 9, 2019
615266e
change tsc outDir to dist, simplify test command
cosmicshuai Oct 9, 2019
b24276c
Merge branch 'master' into shuwan/AddingLG_LSP
boydc2014 Oct 9, 2019
7a792ed
add syntax highlight in demo and new API
cosmicshuai Oct 11, 2019
70729c3
Merge branch 'shuwan/AddingLG_LSP' of https://github.com/microsoft/Bo…
cosmicshuai Oct 11, 2019
79264f0
change naming of the project and move the demo
cosmicshuai Oct 12, 2019
e0927c6
merge from master and resolve confilcts
cosmicshuai Oct 12, 2019
3e35a12
remove package.json in demo
cosmicshuai Oct 12, 2019
1ec29ec
Merge branch 'shuwan/AddingLG_LSP' of https://github.com/microsoft/Bo…
cosmicshuai Oct 12, 2019
ed2a6d7
change the content in readme
cosmicshuai Oct 12, 2019
d3eea2d
fix lg-lsp package publish problem
cosmicshuai Oct 14, 2019
e368b33
fix build command and redundent d.ts files
cosmicshuai Oct 14, 2019
f4e384d
integrate LG LSP server to composer server
cosmicshuai Oct 14, 2019
0fda7c6
change api in demo
cosmicshuai Oct 14, 2019
f8d78a2
Merge branch 'master' into shuwan/AddingLG_LSP
cosmicshuai Oct 14, 2019
a8835be
Merge branch 'master' into shuwan/AddingLG_LSP
boydc2014 Oct 14, 2019
862ddc8
Merge branch 'master' into shuwan/AddingLG_LSP
cosmicshuai Oct 15, 2019
f561ee1
change the order of commands in build:prod
cosmicshuai Oct 15, 2019
67726d1
Merge branch 'shuwan/AddingLG_LSP' of https://github.com/microsoft/Bo…
cosmicshuai Oct 15, 2019
5d58244
delete redundent files generated from build
cosmicshuai Oct 15, 2019
6f03b83
change lg-lsp-server api to attachLSPServer
cosmicshuai Oct 15, 2019
98bb820
remove gitignore in lg-lsp-server demo
cosmicshuai Oct 15, 2019
26d6ae0
Merge branch 'master' into shuwan/AddingLG_LSP
cosmicshuai Oct 17, 2019
57a7e47
Merge branch 'master' into shuwan/AddingLG_LSP
cosmicshuai Oct 22, 2019
54662d7
remove attachLSPServer in server
cosmicshuai Oct 22, 2019
ebc3218
Merge branch 'master' into shuwan/AddingLG_LSP
cosmicshuai Oct 29, 2019
9e36bd3
Merge branch 'master' into shuwan/AddingLG_LSP
zhixzhan Nov 1, 2019
f4732c2
Merge branch 'master' into shuwan/AddingLG_LSP
zhixzhan Nov 1, 2019
64710d7
stash
zhixzhan Nov 1, 2019
eacdd63
freeze vscode-languageserver-protocol@3.15.0-next.8
zhixzhan Nov 3, 2019
c29ab72
stash code-editor lsp demo
zhixzhan Nov 3, 2019
7438c44
update
zhixzhan Nov 4, 2019
bf40556
build react sample demo
zhixzhan Nov 4, 2019
f48b4d4
Merge branch 'lg-lsp' of https://github.com/zhixzhan/BotFramework-Com…
zhixzhan Nov 4, 2019
295e83e
version 17
zhixzhan Nov 4, 2019
470ecc0
launch lsp server
zhixzhan Nov 4, 2019
68c5e53
connect server
zhixzhan Nov 4, 2019
a23cd6c
calc offset
zhixzhan Nov 4, 2019
d67628e
concat full content
zhixzhan Nov 5, 2019
ff018ac
clean demo
zhixzhan Nov 5, 2019
8e71a1e
update
zhixzhan Nov 5, 2019
877f1d3
do not build lglsp editor
zhixzhan Nov 5, 2019
60b07ef
update demo
zhixzhan Nov 5, 2019
6feafd8
update before build
zhixzhan Nov 5, 2019
351ee2d
clean build
zhixzhan Nov 5, 2019
bc717dc
inline mode
zhixzhan Nov 5, 2019
11074df
build
zhixzhan Nov 5, 2019
865e03f
lsp working demo in composer
zhixzhan Nov 5, 2019
8ab31a9
renaming
zhixzhan Nov 5, 2019
9679c0a
eslint and build fix
zhixzhan Nov 6, 2019
181e335
add utils
zhixzhan Nov 6, 2019
77a6972
monaco editor core component
zhixzhan Nov 6, 2019
2880b43
wrap up editor component
zhixzhan Nov 7, 2019
0228e08
seperate client and server
zhixzhan Nov 11, 2019
bc7db19
update wrap
zhixzhan Nov 11, 2019
57c01f1
attach language server to composer server
zhixzhan Nov 11, 2019
f23037e
Merge branch 'master' of https://github.com/microsoft/BotFramework-Co…
zhixzhan Nov 12, 2019
d0d77f8
token rules and suggestion context awareness
zhixzhan Nov 12, 2019
551b707
complete demo
zhixzhan Nov 12, 2019
e6cc0c8
inline template editor in form
zhixzhan Nov 12, 2019
c69c399
inline in all-up view
zhixzhan Nov 12, 2019
7bcdbae
refactor
zhixzhan Nov 12, 2019
35d8e19
Merge branch 'master' of https://github.com/microsoft/BotFramework-Co…
zhixzhan Nov 12, 2019
8bf5a47
check file
zhixzhan Nov 12, 2019
13e9902
update
zhixzhan Nov 13, 2019
a04fffd
remove monaco-webpack-plugin
zhixzhan Nov 13, 2019
f6215de
import diectly instead of call register
zhixzhan Nov 13, 2019
00eed81
create language server url
zhixzhan Nov 13, 2019
e729dbb
add package
zhixzhan Nov 13, 2019
4f05a28
allow suggestion in plaintext state
zhixzhan Nov 13, 2019
17780b7
update integrity hash
zhixzhan Nov 14, 2019
24a9de8
exist check
zhixzhan Nov 14, 2019
ed23287
add back monaco-webpack-plugin
zhixzhan Nov 14, 2019
94e69d7
add mixed demo
zhixzhan Nov 14, 2019
7604244
code refactor
zhixzhan Nov 14, 2019
fd94093
merge folder
zhixzhan Nov 14, 2019
7341bfa
add dependency
zhixzhan Nov 14, 2019
dadc451
resolve alias in jest
zhixzhan Nov 15, 2019
2ba04a6
Merge branch 'master' of https://github.com/microsoft/BotFramework-Co…
zhixzhan Nov 15, 2019
879200c
fix type
zhixzhan Nov 15, 2019
a7861ef
resolve typing
zhixzhan Nov 15, 2019
76eab47
clean up
zhixzhan Nov 15, 2019
1c7c77f
clean up
zhixzhan Nov 15, 2019
e9bbde4
Merge branch 'master' into lg-lsp
cwhitten Nov 16, 2019
f3e4325
Update Composer/package.json
zhixzhan Nov 17, 2019
53e317d
Update Composer/packages/tools/language-servers/language-generation/s…
zhixzhan Nov 17, 2019
77f2855
Update Composer/packages/tools/language-servers/package.json
zhixzhan Nov 17, 2019
353451e
refactor lsp server
zhixzhan Nov 17, 2019
302c170
refactor lsp client
zhixzhan Nov 17, 2019
3e5c218
resolve eslint
zhixzhan Nov 18, 2019
7ea4c10
Merge branch 'master' into lg-lsp
cwhitten Nov 18, 2019
0c62b91
move static syntax setting to code editor
zhixzhan Nov 19, 2019
affcb84
clean up
zhixzhan Nov 19, 2019
2002784
clean
zhixzhan Nov 19, 2019
0334668
use latest @types/vscode replace vscode
zhixzhan Nov 19, 2019
fc61812
Merge branch 'master' of https://github.com/microsoft/BotFramework-Co…
zhixzhan Nov 19, 2019
f700a8a
only path map actual vscode module
a-b-r-o-w-n Nov 19, 2019
94bfc55
disable lint errors
a-b-r-o-w-n Nov 19, 2019
b749264
Merge branch 'master' into lg-lsp
cwhitten Nov 19, 2019
b4decb6
resolve code review warning
zhixzhan Nov 20, 2019
dcaae5e
Merge branch 'master' of https://github.com/microsoft/BotFramework-Co…
zhixzhan Nov 20, 2019
da078cc
clean up
zhixzhan Nov 20, 2019
9e16753
pass less data
zhixzhan Nov 20, 2019
3fb873b
fix type
zhixzhan Nov 20, 2019
000aab4
clean up, typo, refine and naming
zhixzhan Nov 22, 2019
e3f9e58
clean up
zhixzhan Nov 22, 2019
8ca649e
Merge branch 'master' of https://github.com/microsoft/BotFramework-Co…
zhixzhan Nov 22, 2019
88b2feb
extends base
zhixzhan Nov 22, 2019
3428893
hide source name
zhixzhan Nov 22, 2019
681b7d8
add test
zhixzhan Nov 22, 2019
7ca55cc
eslint resolve
zhixzhan Nov 22, 2019
5566272
close connection when document close
zhixzhan Nov 22, 2019
ec4a267
Merge branch 'master' into lg-lsp
a-b-r-o-w-n Nov 22, 2019
1c6c998
Merge branch 'master' into lg-lsp
zhixzhan Nov 23, 2019
e422c11
update
zhixzhan Nov 23, 2019
94f2ea8
add time stamp for every new connection
zhixzhan Nov 25, 2019
0ca7a11
pass parameters to inline editor
zhixzhan Nov 25, 2019
11dfaa3
match lsp server port with env server port
zhixzhan Nov 25, 2019
2504159
rename /lgServer to /lg-language-server
zhixzhan Nov 25, 2019
e1d7b88
refactor code
zhixzhan Nov 25, 2019
5cd9b50
compatible LG Parser, prevent crash
zhixzhan Nov 25, 2019
ffdeffb
do not close shared connection
zhixzhan Nov 25, 2019
5d86a01
send initial diagnostics with delay
zhixzhan Nov 25, 2019
48fbfae
update demo
zhixzhan Nov 26, 2019
31347d4
refactor error handling
zhixzhan Nov 26, 2019
43cf70d
refine, review
zhixzhan Nov 26, 2019
03dcff0
error handling
zhixzhan Nov 26, 2019
677c7e6
ignore non-exist references at check template body
zhixzhan Nov 26, 2019
1a24ecc
update tokenizer
zhixzhan Nov 26, 2019
e5f9bd8
close websocket when editor unmount
zhixzhan Nov 26, 2019
46a7103
all editors use a shared unique ws connection
zhixzhan Nov 27, 2019
0615cfa
Merge branch 'master' into lg-lsp
zhixzhan Nov 27, 2019
7593735
fix structure lg in inline mode and comment token
cosmicshuai Nov 28, 2019
4690635
fix lint
zhixzhan Nov 28, 2019
4ca1ae4
update structure name token
cosmicshuai Nov 28, 2019
f90abe6
fix tokens
cosmicshuai Nov 28, 2019
4908f3c
fix fence_block miss recognize template name
cosmicshuai Nov 28, 2019
89f2aba
remove plaintext in allowState for LG completion
cosmicshuai Nov 28, 2019
2d52722
control initial diagnostics send
zhixzhan Nov 28, 2019
f68709f
Merge branch 'lg-lsp' of https://github.com/zhixzhan/BotFramework-Com…
zhixzhan Nov 28, 2019
5824cef
fix regExp in getWordAtPosition
cosmicshuai Nov 28, 2019
8ff61f2
Merge branch 'lg-lsp' of https://github.com/zhixzhan/BotFramework-Com…
cosmicshuai Nov 28, 2019
7a3aac9
optimize data passing
zhixzhan Nov 28, 2019
1b753ec
update test
zhixzhan Nov 28, 2019
fdde777
Merge branch 'master' into lg-lsp
zhixzhan Nov 29, 2019
979e19e
eslint fix
zhixzhan Nov 29, 2019
9f36281
update test
zhixzhan Nov 29, 2019
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
41 changes: 37 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "attach",
"name": "Attach to Chrome",
"port": 9222,
"webRoot": "${workspaceFolder}"
},
{
"name": "LSP Server",
"type": "node",
"request": "launch",
"args": [
"${workspaceFolder}/Composer/packages/tools/language-servers/language-generation/demo/src/server.ts"
],
"runtimeArgs": [
"--nolazy",
"-r",
"${workspaceFolder}/Composer/node_modules/ts-node/register"
],
"sourceMaps": true,
"cwd": "${workspaceFolder}/Composer/packages/tools/language-servers/language-generation/demo/src",
"protocol": "inspector",
},
{
"type": "node",
"request": "launch",
"name": "Server: Launch",
"args": ["./build/server.js"],
"args": [
"./build/server.js"
],
"preLaunchTask": "server: build",
"restart": true,
"outFiles": ["./build/*"],
"outFiles": [
"./build/*"
],
"envFile": "${workspaceFolder}/Composer/packages/server/.env",
"outputCapture": "std",
"cwd": "${workspaceFolder}/Composer/packages/server"
Expand All @@ -20,8 +47,14 @@
"name": "Jest Debug",
"program": "${workspaceRoot}/Composer/node_modules/jest/bin/jest",
"stopOnEntry": false,
"args": ["--runInBand", "--env=jsdom", "--config=jest.config.js"],
"runtimeArgs": ["--inspect-brk"],
"args": [
"--runInBand",
"--env=jsdom",
"--config=jest.config.js"
],
"runtimeArgs": [
"--inspect-brk"
],
"cwd": "${workspaceRoot}/Composer/packages/server",
"sourceMaps": true,
"console": "integratedTerminal"
Expand Down
1 change: 1 addition & 0 deletions Composer/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ module.exports = {
'<rootDir>/packages/extensions/visual-designer',
'<rootDir>/packages/lib/code-editor',
'<rootDir>/packages/lib/shared',
'<rootDir>/packages/tools/language-servers/language-generation',
],
};
10 changes: 7 additions & 3 deletions Composer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
"packages/extensions",
"packages/extensions/*",
"packages/lib",
"packages/lib/*"
"packages/lib/*",
"packages/tools",
"packages/tools/language-servers",
"packages/tools/language-servers/*"
],
"scripts": {
"build": "node scripts/begin.js && yarn build:prod",
"build:prod": "yarn build:lib && yarn build:extensions && yarn build:server && yarn build:client",
"build:dev": "yarn build:lib && yarn build:extensions",
"build:prod": "yarn build:dev && yarn build:server && yarn build:client",
"build:dev": "yarn build:tools && yarn build:lib && yarn build:extensions",
"build:lib": "yarn workspace @bfc/libs build:all",
"build:extensions": "yarn workspace @bfc/extensions build:all",
"build:server": "yarn workspace @bfc/server build",
"build:client": "yarn workspace @bfc/client build",
"build:tools": "yarn workspace @bfc/tools build:all",
"start": "cross-env NODE_ENV=production PORT=3000 yarn start:server",
"startall": "concurrently --kill-others-on-fail \"npm:runtime\" \"npm:start\"",
"start:dev": "concurrently \"npm:start:client\" \"npm:start:server:dev\"",
Expand Down
6 changes: 5 additions & 1 deletion Composer/packages/client/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ module.exports = function(webpackEnv) {
// `web` extension prefixes have been added for better support
// for React Native Web.
extensions: paths.moduleFileExtensions.map(ext => `.${ext}`),
alias: {
// Support lsp code editor
vscode: require.resolve('monaco-languageclient/lib/vscode-compatibility'),
},
plugins: [
// Adds support for installing with Plug'n'Play, leading to faster installs and adding
// guards against forgotten dependencies and such.
Expand Down Expand Up @@ -402,7 +406,7 @@ module.exports = function(webpackEnv) {
plugins: [
new MonacoWebpackPlugin({
// available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
languages: ['markdown', 'botbuilderlg', 'json'],
languages: ['markdown', 'json'],
}),
// Generates an `index.html` file with the <script> injected.
new HtmlWebpackPlugin(
Expand Down
2 changes: 2 additions & 0 deletions Composer/packages/client/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module.exports = {
// Any imports of .scss / .css files will instead import styleMock.js which is an empty object
'\\.(jpg|jpeg|png|svg|gif)$': '<rootDir>/__tests__/jestMocks/styleMock.js',
'\\.(s)?css$': '<rootDir>/__tests__/jestMocks/styleMock.js',
// lsp code editor
vscode$: 'monaco-languageclient/lib/vscode-compatibility',

// use commonjs modules for test so they do not need to be compiled
'office-ui-fabric-react/lib/(.*)$': 'office-ui-fabric-react/lib-commonjs/$1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,108 @@
// Licensed under the MIT License.

/* eslint-disable react/display-name */
import React, { useState, useEffect } from 'react';
import { LgEditor } from '@bfc/code-editor';
import React, { useState, useEffect, useMemo, useContext } from 'react';
import { LgEditor, LGOption } from '@bfc/code-editor';
import get from 'lodash/get';
import debounce from 'lodash/debounce';
import isEmpty from 'lodash/isEmpty';
import { CodeRange, LgFile } from '@bfc/shared';
import { Diagnostic } from 'botbuilder-lg';
import { LgFile } from '@bfc/shared';

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

interface CodeEditorProps {
file: LgFile;
onChange: (value: string) => void;
codeRange?: Partial<CodeRange> | null;
template: lgUtil.Template | null;
}

// lsp server port should be same with composer/server port.
const lspServerPort = process.env.NODE_ENV === 'production' ? process.env.PORT || 3000 : 5000;

Choose a reason for hiding this comment

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

If this code belongs on client-side (browser), it shouldn't be able to access NODE_ENV.

Copy link
Member

Choose a reason for hiding this comment

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

William we use a webpack plugin to serialize process.env into the client. No sensitive information is expected.

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

export default function CodeEditor(props: CodeEditorProps) {
const { file, codeRange } = props;
const onChange = debounce(props.onChange, 500);
const { actions } = useContext(StoreContext);
const { file, template } = props;
const [diagnostics, setDiagnostics] = useState(get(file, 'diagnostics', []));
const [content, setContent] = useState(get(file, 'content', ''));

const [content, setContent] = useState('');
const [errorMsg, setErrorMsg] = useState('');
const fileId = file && file.id;
const inlineMode = !!template;
useEffect(() => {
// reset content with file.content's initial state
if (isEmpty(file)) return;
setContent(file.content);
}, [fileId]);
const value = template ? get(template, 'Body', '') : get(file, 'content', '');
setContent(value);
}, [fileId, template]);

useEffect(() => {
const isInvalid = !lgUtil.isValid(diagnostics);
const text = isInvalid ? lgUtil.combineMessage(diagnostics) : '';
setErrorMsg(text);
}, [diagnostics]);

const updateLgTemplate = useMemo(
() =>
debounce((body: string) => {
const templateName = get(template, 'Name');
if (!templateName) return;
const payload = {
file,
templateName,
template: {
Name: templateName,
Parameters: get(template, 'Parameters'),
Body: body,
},
};
actions.updateLgTemplate(payload);
}, 500),
[file, template]
);

const updateLgFile = useMemo(
() =>
debounce((content: string) => {
const payload = {
id: file.id,
content,
};
actions.updateLgFile(payload);
}, 500),
[file]
);

// local content maybe invalid and should always sync real-time
// file.content assume to be load from server
const _onChange = value => {
setContent(value);
onChange(value);
const diagnostics = lgUtil.check(value);

let diagnostics: Diagnostic[] = [];
if (inlineMode) {
const content = get(file, 'content', '');
const templateName = get(template, 'Name', '');
try {
const newContent = lgUtil.updateTemplate(content, templateName, {
Name: templateName,
Parameters: get(template, 'Parameters'),
Body: value,
});
diagnostics = lgUtil.check(newContent);
updateLgTemplate(value);
} catch (error) {
setErrorMsg(error.message);
}
} else {
diagnostics = lgUtil.check(value);
updateLgFile(value);
}
setDiagnostics(diagnostics);
};

const isInvalid = !lgUtil.isValid(diagnostics);
const errorMsg = isInvalid ? lgUtil.combineMessage(diagnostics) : '';
const lgOption: LGOption = {
inline: inlineMode,
content: get(file, 'content', ''),
template: template ? template : undefined,
};

return (
<LgEditor
Expand All @@ -53,9 +116,14 @@ export default function CodeEditor(props: CodeEditorProps) {
lineDecorationsWidth: undefined,
lineNumbersMinChars: false,
}}
codeRange={codeRange || -1}
errorMsg={errorMsg}
hidePlaceholder={inlineMode}
value={content}
errorMsg={errorMsg}
lgOption={lgOption}
languageServer={{
port: Number(lspServerPort),
path: lspServerPath,
}}
onChange={_onChange}
/>
);
Expand Down
43 changes: 12 additions & 31 deletions Composer/packages/client/src/pages/language-generation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import React, { useContext, Fragment, useEffect, useState, useMemo, Suspense } f
import formatMessage from 'format-message';
import { Toggle } from 'office-ui-fabric-react/lib/Toggle';
import { Nav, INavLinkGroup, INavLink } from 'office-ui-fabric-react/lib/Nav';
import get from 'lodash/get';
import { LGTemplate } from 'botbuilder-lg';
import { RouteComponentProps } from '@reach/router';
import { CodeRange } from '@bfc/shared';
import get from 'lodash/get';

import { LoadingSpinner } from '../../components/LoadingSpinner';
import { OpenAlertModal, DialogStyle } from '../../components/Modal';
import { StoreContext } from '../../store';
import {
ContentHeaderStyle,
Expand All @@ -33,11 +32,11 @@ import { TestController } from './../../TestController';
const CodeEditor = React.lazy(() => import('./code-editor'));

const LGPage: React.FC<RouteComponentProps> = props => {
const { state, actions } = useContext(StoreContext);
const { state } = useContext(StoreContext);
const { lgFiles, dialogs } = state;
const [editMode, setEditMode] = useState(false);
const [fileValid, setFileValid] = useState(true);
const [codeRange, setCodeRange] = useState<CodeRange | null>(null);
const [inlineTemplate, setInlineTemplate] = useState<null | lgUtil.Template>(null);

const subPath = props['*'];
const isRoot = subPath === '';
Expand Down Expand Up @@ -121,34 +120,16 @@ const LGPage: React.FC<RouteComponentProps> = props => {

function onToggleEditMode() {
setEditMode(!editMode);
setCodeRange(null);
}

async function onChange(newContent) {
if (!lgFile) {
return;
}

const payload = {
id: lgFile.id,
content: newContent,
};

try {
await actions.updateLgFile(payload);
} catch (error) {
OpenAlertModal('Save Failed', error.message, {
style: DialogStyle.Console,
});
}
setInlineTemplate(null);
}

// #TODO: get line number from lg parser, then deep link to code editor this
// Line
function onTableViewClickEdit(template) {
setCodeRange({
startLineNumber: get(template, 'ParseTree._start._line', 0),
endLineNumber: get(template, 'ParseTree._stop._line', 0),
function onTableViewClickEdit(template: LGTemplate) {
setInlineTemplate({
Name: get(template, 'Name', ''),
Parameters: get(template, 'Parameters'),
Body: get(template, 'Body', ''),
});
navigateTo(`/language-generation`);
setEditMode(true);
Expand All @@ -174,7 +155,7 @@ const LGPage: React.FC<RouteComponentProps> = props => {
onText={formatMessage('Edit mode')}
offText={formatMessage('Edit mode')}
checked={editMode}
disabled={(!isRoot && editMode === false) || (codeRange === null && fileValid === false)}
disabled={(!isRoot && editMode === false) || (fileValid === false && editMode === true)}
onChange={onToggleEditMode}
/>
</div>
Expand Down Expand Up @@ -215,7 +196,7 @@ const LGPage: React.FC<RouteComponentProps> = props => {
<div css={contentEditor}>
{editMode ? (
<Suspense fallback={<LoadingSpinner />}>
<CodeEditor file={lgFile} codeRange={codeRange} onChange={onChange} />
<CodeEditor file={lgFile} template={inlineTemplate} />
</Suspense>
) : (
<TableView file={lgFile} activeDialog={activeDialog} onClickEdit={onTableViewClickEdit} />
Expand Down
12 changes: 8 additions & 4 deletions Composer/packages/client/src/utils/lgUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const lgStaticChecker = new StaticChecker();

const lgImportResolver = ImportResolver.fileResolver;

interface Template {
export interface Template {
Name: string;
Parameters: string[];
Parameters?: string[];
Body: string;
}

Expand Down Expand Up @@ -69,7 +69,11 @@ export function increaseNameUtilNotExist(templates: LGTemplate[], name: string):
return newName;
}

export function updateTemplate(content: string, templateName: string, { Name, Parameters, Body }: Template): string {
export function updateTemplate(
content: string,
templateName: string,
{ Name, Parameters = [], Body }: Template
): string {
const resource = LGParser.parse(content);
// add if not exist
if (resource.Templates.findIndex(t => t.Name === templateName) === -1) {
Expand All @@ -80,7 +84,7 @@ export function updateTemplate(content: string, templateName: string, { Name, Pa
}

// if Name exist, throw error.
export function addTemplate(content: string, { Name, Parameters, Body }: Template): string {
export function addTemplate(content: string, { Name, Parameters = [], Body }: Template): string {
const resource = LGParser.parse(content);
return resource.addTemplate(Name, Parameters, Body).toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ module.exports = {
resolve: {
extensions: ['.tsx', '.ts', '.js'],
plugins: [new TsconfigPathsPlugin({ configFile: path.resolve(__dirname, './tsconfig.json') })],
alias: {
vscode: require.resolve('monaco-languageclient/lib/vscode-compatibility'),
},
},

output: {
Expand Down
Loading