Skip to content

Commit

Permalink
fix #5229: Add language configuration items for js, ts and jsx
Browse files Browse the repository at this point in the history
Signed-off-by: Brokun <[email protected]>
  • Loading branch information
BroKun committed Sep 16, 2019
1 parent dbb25e0 commit fe69fb7
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/textmate-grammars/src/browser/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { injectable } from 'inversify';
import { LanguageGrammarDefinitionContribution, TextmateRegistry, getEncodedLanguageId, GrammarDefinition } from '@theia/monaco/lib/browser/textmate';
Expand Down Expand Up @@ -117,6 +121,45 @@ export class JavascriptContribution implements LanguageGrammarDefinitionContribu
}

protected configuration: monaco.languages.LanguageConfiguration = {
// copied and modified from https://github.com/microsoft/vscode/blob/master/extensions/typescript-language-features/src/features/languageConfiguration.ts
'indentationRules': {
'decreaseIndentPattern': /^((?!.*?\/\*).*\*\/)?\s*[\}\]].*$/,
'increaseIndentPattern': /^((?!\/\/).)*(\{[^}"'`]*|\([^)"'`]*|\[[^\]"'`]*)$/
},
'wordPattern': /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
'onEnterRules': [
{
// e.g. /** | */
'beforeText': /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
'afterText': /^\s*\*\/$/,
'action': { indentAction: monaco.languages.IndentAction.IndentOutdent, appendText: ' * ' },
},
{
// e.g. /** ...|
'beforeText': /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
'action': { indentAction: monaco.languages.IndentAction.None, appendText: ' * ' },
},
{
// e.g. * ...|
'beforeText': /^(\t|[ ])*[ ]\*([ ]([^\*]|\*(?!\/))*)?$/,
'action': { indentAction: monaco.languages.IndentAction.None, appendText: '* ' },
},
{
// e.g. */|
'beforeText': /^(\t|[ ])*[ ]\*\/\s*$/,
'action': { indentAction: monaco.languages.IndentAction.None, removeText: 1 },
},
{
// e.g. *-----*/|
'beforeText': /^(\t|[ ])*[ ]\*[^/]*\*\/\s*$/,
'action': { indentAction: monaco.languages.IndentAction.None, removeText: 1 },
},
{
'beforeText': /^\s*(\bcase\s.+:|\bdefault:)$/,
'afterText': /^(?!\s*(\bcase\b|\bdefault\b))/,
'action': { indentAction: monaco.languages.IndentAction.Indent },
}
],
'comments': {
'lineComment': '//',
'blockComment': ['/*', '*/']
Expand Down
31 changes: 31 additions & 0 deletions packages/textmate-grammars/src/browser/jsx-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { injectable } from 'inversify';
import { LanguageGrammarDefinitionContribution, TextmateRegistry } from '@theia/monaco/lib/browser/textmate';

@injectable()
export class JsxTagsContribution implements LanguageGrammarDefinitionContribution {
private readonly id = 'jsx-tags';
// copied and modified from https://github.com/microsoft/vscode/blob/master/extensions/typescript-language-features/src/features/languageConfiguration.ts
static EMPTY_ELEMENTS: string[] = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'];

registerTextmateLanguage(registry: TextmateRegistry): void {
this.registerJsxTags();
Expand All @@ -36,6 +42,31 @@ export class JsxTagsContribution implements LanguageGrammarDefinitionContributio
}

protected configuration: monaco.languages.LanguageConfiguration = {
// copied and modified from https://github.com/microsoft/vscode/blob/master/extensions/typescript-language-features/src/features/languageConfiguration.ts
'wordPattern': /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
'onEnterRules': [
{
'beforeText': new RegExp(`<(?!(?:${JsxTagsContribution.EMPTY_ELEMENTS.join('|')}))([_:\\w][_:\\w\\-.\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
'afterText': /^<\/([_:\w][_:\w-.\d]*)\s*>$/i,
'action': { indentAction: monaco.languages.IndentAction.IndentOutdent }
},
{
'beforeText': new RegExp(`<(?!(?:${JsxTagsContribution.EMPTY_ELEMENTS.join('|')}))([_:\\w][_:\\w\\-.\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
'action': { indentAction: monaco.languages.IndentAction.Indent }
},
{
// `beforeText` only applies to tokens of a given language. Since we are dealing with jsx-tags,
// make sure we apply to the closing `>` of a tag so that mixed language spans
// such as `<div onclick={1}>` are handled properly.
'beforeText': /^>$/,
'afterText': /^<\/([_:\w][_:\w-.\d]*)\s*>$/i,
'action': { indentAction: monaco.languages.IndentAction.IndentOutdent }
},
{
'beforeText': /^>$/,
'action': { indentAction: monaco.languages.IndentAction.Indent }
},
],
'comments': {
'blockComment': ['{/*', '*/}']
},
Expand Down
43 changes: 43 additions & 0 deletions packages/textmate-grammars/src/browser/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { injectable, inject } from 'inversify';
import { LanguageGrammarDefinitionContribution, TextmateRegistry, GrammarDefinition } from '@theia/monaco/lib/browser/textmate';
Expand Down Expand Up @@ -106,6 +110,45 @@ export class TypescriptContribution implements LanguageGrammarDefinitionContribu
}

protected configuration: monaco.languages.LanguageConfiguration = {
// copied and modified from https://github.com/microsoft/vscode/blob/master/extensions/typescript-language-features/src/features/languageConfiguration.ts
'indentationRules': {
'decreaseIndentPattern': /^((?!.*?\/\*).*\*\/)?\s*[\}\]].*$/,
'increaseIndentPattern': /^((?!\/\/).)*(\{[^}"'`]*|\([^)"'`]*|\[[^\]"'`]*)$/
},
'wordPattern': /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
'onEnterRules': [
{
// e.g. /** | */
'beforeText': /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
'afterText': /^\s*\*\/$/,
'action': { indentAction: monaco.languages.IndentAction.IndentOutdent, appendText: ' * ' },
},
{
// e.g. /** ...|
'beforeText': /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
'action': { indentAction: monaco.languages.IndentAction.None, appendText: ' * ' },
},
{
// e.g. * ...|
'beforeText': /^(\t|[ ])*[ ]\*([ ]([^\*]|\*(?!\/))*)?$/,
'action': { indentAction: monaco.languages.IndentAction.None, appendText: '* ' },
},
{
// e.g. */|
'beforeText': /^(\t|[ ])*[ ]\*\/\s*$/,
'action': { indentAction: monaco.languages.IndentAction.None, removeText: 1 },
},
{
// e.g. *-----*/|
'beforeText': /^(\t|[ ])*[ ]\*[^/]*\*\/\s*$/,
'action': { indentAction: monaco.languages.IndentAction.None, removeText: 1 },
},
{
'beforeText': /^\s*(\bcase\s.+:|\bdefault:)$/,
'afterText': /^(?!\s*(\bcase\b|\bdefault\b))/,
'action': { indentAction: monaco.languages.IndentAction.Indent },
}
],
'comments': {
'lineComment': '//',
'blockComment': ['/*', '*/']
Expand Down

0 comments on commit fe69fb7

Please sign in to comment.