提交 12e35dfb 编写于 作者: R rebornix

Customize indent rules in language configuration

上级 c072037b
......@@ -21,5 +21,9 @@
["(", ")"],
["\"", "\""],
["'", "'"]
]
],
"indentationRules": {
"increaseIndentPattern": "^\\s*((begin|class|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|while)|(.*\\sdo\\b))\\b[^\\{;]*$",
"decreaseIndentPattern": "^\\s*([}\\]]([,)]?\\s*(#|$)|\\.[a-zA-Z_]\\w*\\b)|(end|rescue|ensure|else|elsif|when)\\b)"
}
}
\ No newline at end of file
{
"name": "ruby",
"version": "0.2.0",
"version": "0.2.1",
"publisher": "vscode",
"engines": { "vscode": "*" },
"activationEvents": ["onLanguage:ruby"],
"main": "./out/rubyMain",
"contributes": {
"languages": [{
"id": "ruby",
......@@ -19,10 +17,5 @@
"scopeName": "source.ruby",
"path": "./syntaxes/ruby.tmLanguage.json"
}]
},
"scripts": {
"compile": "gulp compile-extension:ruby",
"watch": "gulp watch-extension:ruby",
"update-grammar": "node ../../build/npm/update-grammar.js textmate/ruby.tmbundle Syntaxes/Ruby.plist ./syntaxes/ruby.tmLanguage.json"
}
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { ExtensionContext, languages } from 'vscode';
export function activate(context: ExtensionContext): any {
languages.setLanguageConfiguration('ruby', {
indentationRules: {
increaseIndentPattern: /^\s*((begin|class|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|while)|(.*\sdo\b))\b[^\{;]*$/,
decreaseIndentPattern: /^\s*([}\]]([,)]?\s*(#|$)|\.[a-zA-Z_]\w*\b)|(end|rescue|ensure|else|elsif|when)\b)/
}
});
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/// <reference path='../../../../src/vs/vscode.d.ts'/>
\ No newline at end of file
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "./out",
"lib": [
"es2015"
],
"sourceMap": true
},
"include": [
"src/**/*"
]
}
\ No newline at end of file
......@@ -7,7 +7,7 @@
import * as nls from 'vs/nls';
import { parse, ParseError } from 'vs/base/common/json';
import { readFile } from 'vs/base/node/pfs';
import { CharacterPair, LanguageConfiguration, IAutoClosingPair, IAutoClosingPairConditional, CommentRule } from 'vs/editor/common/modes/languageConfiguration';
import { CharacterPair, LanguageConfiguration, IAutoClosingPair, IAutoClosingPairConditional, IndentationRule, CommentRule } from 'vs/editor/common/modes/languageConfiguration';
import { IModeService } from 'vs/editor/common/services/modeService';
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
import { Extensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
......@@ -21,12 +21,20 @@ interface IRegExp {
flags?: string;
}
interface IIndentationRules {
decreaseIndentPattern: string | IRegExp;
increaseIndentPattern: string | IRegExp;
indentNextLinePattern?: string | IRegExp;
unIndentedLinePattern?: string | IRegExp;
}
interface ILanguageConfiguration {
comments?: CommentRule;
brackets?: CharacterPair[];
autoClosingPairs?: (CharacterPair | IAutoClosingPairConditional)[];
surroundingPairs?: (CharacterPair | IAutoClosingPair)[];
wordPattern?: string | IRegExp;
indentationRules?: IIndentationRules;
}
export class LanguageConfigurationFileHandler {
......@@ -92,26 +100,61 @@ export class LanguageConfigurationFileHandler {
}
if (configuration.wordPattern) {
let pattern = '';
let flags = '';
try {
let wordPattern = this._parseRegex(configuration.wordPattern);
if (wordPattern) {
richEditConfig.wordPattern = wordPattern;
}
} catch (error) {
// Malformed regexes are ignored
}
}
if (typeof configuration.wordPattern === 'string') {
pattern = configuration.wordPattern;
} else if (typeof configuration.wordPattern === 'object') {
pattern = configuration.wordPattern.pattern;
flags = configuration.wordPattern.flags;
if (configuration.indentationRules) {
let indentationRules = this._mapIndentationRules(configuration.indentationRules);
if (indentationRules) {
richEditConfig.indentationRules = indentationRules;
}
}
LanguageConfigurationRegistry.register(languageIdentifier, richEditConfig);
}
private _parseRegex(value: string | IRegExp) {
if (typeof value === 'string') {
return new RegExp(value, '');
} else if (typeof value === 'object') {
return new RegExp(value.pattern, value.flags);
}
if (pattern) {
try {
richEditConfig.wordPattern = new RegExp(pattern, flags);
} catch (error) {
// Malformed regexes are ignored
return null;
}
private _mapIndentationRules(indentationRules: IIndentationRules): IndentationRule {
try {
let increaseIndentPattern = this._parseRegex(indentationRules.increaseIndentPattern);
let decreaseIndentPattern = this._parseRegex(indentationRules.decreaseIndentPattern);
if (increaseIndentPattern && decreaseIndentPattern) {
let result: IndentationRule = {
increaseIndentPattern: increaseIndentPattern,
decreaseIndentPattern: decreaseIndentPattern
};
if (indentationRules.indentNextLinePattern) {
result.indentNextLinePattern = this._parseRegex(indentationRules.indentNextLinePattern);
}
if (indentationRules.unIndentedLinePattern) {
result.unIndentedLinePattern = this._parseRegex(indentationRules.unIndentedLinePattern);
}
return result;
}
} catch (error) {
// Malformed regexes are ignored
}
LanguageConfigurationRegistry.register(languageIdentifier, richEditConfig);
return null;
}
private _mapCharacterPairs(pairs: (CharacterPair | IAutoClosingPairConditional)[]): IAutoClosingPairConditional[] {
......@@ -252,6 +295,88 @@ const schema: IJSONSchema = {
patternErrorMessage: nls.localize('schema.wordPattern.flags.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
}
}
},
indentationRules: {
default: {
increaseIndentPattern: '',
decreaseIndentPattern: ''
},
description: nls.localize('schema.indentationRules', 'The language\'s indentation settings.'),
type: 'object',
properties: {
increaseIndentPattern: {
type: ['string', 'object'],
description: nls.localize('schema.indentationRules.increaseIndentPattern', 'If a line matches this pattern, then all the lines after it should be indented once (until another rule matches).'),
properties: {
pattern: {
type: 'string',
description: nls.localize('schema.indentationRules.increaseIndentPattern.pattern', 'The RegExp pattern for increaseIndentPattern.'),
default: '',
},
flags: {
type: 'string',
description: nls.localize('schema.indentationRules.increaseIndentPattern.flags', 'The RegExp flags for increaseIndentPattern.'),
default: '',
pattern: '^([gimuy]+)$',
patternErrorMessage: nls.localize('schema.indentationRules.increaseIndentPattern.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
}
}
},
decreaseIndentPattern: {
type: ['string', 'object'],
description: nls.localize('schema.indentationRules.decreaseIndentPattern', 'If a line matches this pattern, then all the lines after it should be unindendented once (until another rule matches).'),
properties: {
pattern: {
type: 'string',
description: nls.localize('schema.indentationRules.decreaseIndentPattern.pattern', 'The RegExp pattern for decreaseIndentPattern.'),
default: '',
},
flags: {
type: 'string',
description: nls.localize('schema.indentationRules.decreaseIndentPattern.flags', 'The RegExp flags for decreaseIndentPattern.'),
default: '',
pattern: '^([gimuy]+)$',
patternErrorMessage: nls.localize('schema.indentationRules.decreaseIndentPattern.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
}
}
},
indentNextLinePattern: {
type: ['string', 'object'],
description: nls.localize('schema.indentationRules.indentNextLinePattern', 'If a line matches this pattern, then **only the next line** after it should be indented once.'),
properties: {
pattern: {
type: 'string',
description: nls.localize('schema.indentationRules.indentNextLinePattern.pattern', 'The RegExp pattern for indentNextLinePattern.'),
default: '',
},
flags: {
type: 'string',
description: nls.localize('schema.indentationRules.indentNextLinePattern.flags', 'The RegExp flags for indentNextLinePattern.'),
default: '',
pattern: '^([gimuy]+)$',
patternErrorMessage: nls.localize('schema.indentationRules.indentNextLinePattern.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
}
}
},
unIndentedLinePattern: {
type: ['string', 'object'],
description: nls.localize('schema.indentationRules.unIndentedLinePattern', 'If a line matches this pattern, then its indentation should not be changed and it should not be evaluated against the other rules.'),
properties: {
pattern: {
type: 'string',
description: nls.localize('schema.indentationRules.unIndentedLinePattern.pattern', 'The RegExp pattern for unIndentedLinePattern.'),
default: '',
},
flags: {
type: 'string',
description: nls.localize('schema.indentationRules.unIndentedLinePattern.flags', 'The RegExp flags for unIndentedLinePattern.'),
default: '',
pattern: '^([gimuy]+)$',
patternErrorMessage: nls.localize('schema.indentationRules.unIndentedLinePattern.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
}
}
}
}
}
}
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册