mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
Customize indent rules in language configuration
This commit is contained in:
parent
c072037b4a
commit
12e35dfb3e
|
@ -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)"
|
||||
}
|
||||
}
|
|
@ -1,10 +1,8 @@
|
|||
{
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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)/
|
||||
}
|
||||
});
|
||||
}
|
6
extensions/ruby/src/typings/ref.d.ts
vendored
6
extensions/ruby/src/typings/ref.d.ts
vendored
|
@ -1,6 +0,0 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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'/>
|
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"outDir": "./out",
|
||||
"lib": [
|
||||
"es2015"
|
||||
],
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": [
|
||||
"src/**/*"
|
||||
]
|
||||
}
|
|
@ -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,28 +100,63 @@ export class LanguageConfigurationFileHandler {
|
|||
}
|
||||
|
||||
if (configuration.wordPattern) {
|
||||
let pattern = '';
|
||||
let flags = '';
|
||||
|
||||
if (typeof configuration.wordPattern === 'string') {
|
||||
pattern = configuration.wordPattern;
|
||||
} else if (typeof configuration.wordPattern === 'object') {
|
||||
pattern = configuration.wordPattern.pattern;
|
||||
flags = configuration.wordPattern.flags;
|
||||
}
|
||||
|
||||
if (pattern) {
|
||||
try {
|
||||
richEditConfig.wordPattern = new RegExp(pattern, flags);
|
||||
} catch (error) {
|
||||
// Malformed regexes are ignored
|
||||
try {
|
||||
let wordPattern = this._parseRegex(configuration.wordPattern);
|
||||
if (wordPattern) {
|
||||
richEditConfig.wordPattern = wordPattern;
|
||||
}
|
||||
} catch (error) {
|
||||
// Malformed regexes are ignored
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private _mapCharacterPairs(pairs: (CharacterPair | IAutoClosingPairConditional)[]): IAutoClosingPairConditional[] {
|
||||
return pairs.map(pair => {
|
||||
if (Array.isArray(pair)) {
|
||||
|
@ -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]+)$/`.')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue