Skip to content

Commit

Permalink
(fix) register OnEnter rules (sveltejs#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-fuze committed Jun 28, 2020
1 parent 22304be commit b4dbeff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/svelte-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
Uri,
ProgressLocation,
ViewColumn,
languages,
IndentAction,
} from 'vscode';
import {
LanguageClient,
Expand All @@ -23,6 +25,7 @@ import {
TextDocumentEdit,
} from 'vscode-languageclient';
import { activateTagClosing } from './html/autoClose';
import { EMPTY_ELEMENTS } from './html/htmlEmptyTagsShared';
import CompiledCodeContentProvider from './CompiledCodeContentProvider';
import * as path from 'path';

Expand Down Expand Up @@ -127,6 +130,25 @@ export function activate(context: ExtensionContext) {
addRenameFileListener(getLS);

addCompilePreviewCommand(getLS, context);

languages.setLanguageConfiguration('svelte', {
indentationRules: {
increaseIndentPattern: /<(?!\?|(?:area|base|br|col|frame|hr|html|img|input|link|meta|param)\b|[^>]*\/>)([-_\.A-Za-z0-9]+)(?=\s|>)\b[^>]*>(?!.*<\/\1>)|<!--(?!.*-->)|\{[^}"']*$/,
decreaseIndentPattern: /^\s*(<\/(?!html)[-_\.A-Za-z0-9]+\b[^>]*>|-->|\})/,
},
wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
onEnterRules: [
{
beforeText: new RegExp(`<(?!(?:${EMPTY_ELEMENTS.join('|')}))([_:\\w][_:\\w-.\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
afterText: /^<\/([_:\w][_:\w-.\d]*)\s*>/i,
action: { indentAction: IndentAction.IndentOutdent },
},
{
beforeText: new RegExp(`<(?!(?:${EMPTY_ELEMENTS.join('|')}))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$`, 'i'),
action: { indentAction: IndentAction.Indent },
},
],
});
}

function addRenameFileListener(getLS: () => LanguageClient) {
Expand Down
26 changes: 26 additions & 0 deletions packages/svelte-vscode/src/html/htmlEmptyTagsShared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Original source: https://github.com/microsoft/vscode/blob/master/extensions/html-language-features/client/src/htmlEmptyTagsShared.ts

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export const EMPTY_ELEMENTS: string[] = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'menuitem',
'meta',
'param',
'source',
'track',
'wbr',
];

0 comments on commit b4dbeff

Please sign in to comment.