Skip to content

Commit 12e35df

Browse files
committed
Customize indent rules in language configuration
1 parent c072037 commit 12e35df

6 files changed

Lines changed: 145 additions & 58 deletions

File tree

extensions/ruby/language-configuration.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
["(", ")"],
2222
["\"", "\""],
2323
["'", "'"]
24-
]
24+
],
25+
"indentationRules": {
26+
"increaseIndentPattern": "^\\s*((begin|class|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|while)|(.*\\sdo\\b))\\b[^\\{;]*$",
27+
"decreaseIndentPattern": "^\\s*([}\\]]([,)]?\\s*(#|$)|\\.[a-zA-Z_]\\w*\\b)|(end|rescue|ensure|else|elsif|when)\\b)"
28+
}
2529
}

extensions/ruby/package.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
22
"name": "ruby",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"publisher": "vscode",
55
"engines": { "vscode": "*" },
6-
"activationEvents": ["onLanguage:ruby"],
7-
"main": "./out/rubyMain",
86
"contributes": {
97
"languages": [{
108
"id": "ruby",
@@ -19,10 +17,5 @@
1917
"scopeName": "source.ruby",
2018
"path": "./syntaxes/ruby.tmLanguage.json"
2119
}]
22-
},
23-
"scripts": {
24-
"compile": "gulp compile-extension:ruby",
25-
"watch": "gulp watch-extension:ruby",
26-
"update-grammar": "node ../../build/npm/update-grammar.js textmate/ruby.tmbundle Syntaxes/Ruby.plist ./syntaxes/ruby.tmLanguage.json"
2720
}
2821
}

extensions/ruby/src/rubyMain.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

extensions/ruby/src/typings/ref.d.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

extensions/ruby/tsconfig.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/vs/workbench/parts/codeEditor/electron-browser/languageConfiguration/languageConfigurationExtensionPoint.ts

Lines changed: 139 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import * as nls from 'vs/nls';
88
import { parse, ParseError } from 'vs/base/common/json';
99
import { readFile } from 'vs/base/node/pfs';
10-
import { CharacterPair, LanguageConfiguration, IAutoClosingPair, IAutoClosingPairConditional, CommentRule } from 'vs/editor/common/modes/languageConfiguration';
10+
import { CharacterPair, LanguageConfiguration, IAutoClosingPair, IAutoClosingPairConditional, IndentationRule, CommentRule } from 'vs/editor/common/modes/languageConfiguration';
1111
import { IModeService } from 'vs/editor/common/services/modeService';
1212
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
1313
import { Extensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
@@ -21,12 +21,20 @@ interface IRegExp {
2121
flags?: string;
2222
}
2323

24+
interface IIndentationRules {
25+
decreaseIndentPattern: string | IRegExp;
26+
increaseIndentPattern: string | IRegExp;
27+
indentNextLinePattern?: string | IRegExp;
28+
unIndentedLinePattern?: string | IRegExp;
29+
}
30+
2431
interface ILanguageConfiguration {
2532
comments?: CommentRule;
2633
brackets?: CharacterPair[];
2734
autoClosingPairs?: (CharacterPair | IAutoClosingPairConditional)[];
2835
surroundingPairs?: (CharacterPair | IAutoClosingPair)[];
2936
wordPattern?: string | IRegExp;
37+
indentationRules?: IIndentationRules;
3038
}
3139

3240
export class LanguageConfigurationFileHandler {
@@ -92,26 +100,61 @@ export class LanguageConfigurationFileHandler {
92100
}
93101

94102
if (configuration.wordPattern) {
95-
let pattern = '';
96-
let flags = '';
103+
try {
104+
let wordPattern = this._parseRegex(configuration.wordPattern);
105+
if (wordPattern) {
106+
richEditConfig.wordPattern = wordPattern;
107+
}
108+
} catch (error) {
109+
// Malformed regexes are ignored
110+
}
111+
}
97112

98-
if (typeof configuration.wordPattern === 'string') {
99-
pattern = configuration.wordPattern;
100-
} else if (typeof configuration.wordPattern === 'object') {
101-
pattern = configuration.wordPattern.pattern;
102-
flags = configuration.wordPattern.flags;
113+
if (configuration.indentationRules) {
114+
let indentationRules = this._mapIndentationRules(configuration.indentationRules);
115+
if (indentationRules) {
116+
richEditConfig.indentationRules = indentationRules;
103117
}
118+
}
119+
120+
LanguageConfigurationRegistry.register(languageIdentifier, richEditConfig);
121+
}
122+
123+
private _parseRegex(value: string | IRegExp) {
124+
if (typeof value === 'string') {
125+
return new RegExp(value, '');
126+
} else if (typeof value === 'object') {
127+
return new RegExp(value.pattern, value.flags);
128+
}
104129

105-
if (pattern) {
106-
try {
107-
richEditConfig.wordPattern = new RegExp(pattern, flags);
108-
} catch (error) {
109-
// Malformed regexes are ignored
130+
return null;
131+
}
132+
133+
private _mapIndentationRules(indentationRules: IIndentationRules): IndentationRule {
134+
try {
135+
let increaseIndentPattern = this._parseRegex(indentationRules.increaseIndentPattern);
136+
let decreaseIndentPattern = this._parseRegex(indentationRules.decreaseIndentPattern);
137+
138+
if (increaseIndentPattern && decreaseIndentPattern) {
139+
let result: IndentationRule = {
140+
increaseIndentPattern: increaseIndentPattern,
141+
decreaseIndentPattern: decreaseIndentPattern
142+
};
143+
144+
if (indentationRules.indentNextLinePattern) {
145+
result.indentNextLinePattern = this._parseRegex(indentationRules.indentNextLinePattern);
146+
}
147+
if (indentationRules.unIndentedLinePattern) {
148+
result.unIndentedLinePattern = this._parseRegex(indentationRules.unIndentedLinePattern);
110149
}
150+
151+
return result;
111152
}
153+
} catch (error) {
154+
// Malformed regexes are ignored
112155
}
113156

114-
LanguageConfigurationRegistry.register(languageIdentifier, richEditConfig);
157+
return null;
115158
}
116159

117160
private _mapCharacterPairs(pairs: (CharacterPair | IAutoClosingPairConditional)[]): IAutoClosingPairConditional[] {
@@ -252,6 +295,88 @@ const schema: IJSONSchema = {
252295
patternErrorMessage: nls.localize('schema.wordPattern.flags.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
253296
}
254297
}
298+
},
299+
indentationRules: {
300+
default: {
301+
increaseIndentPattern: '',
302+
decreaseIndentPattern: ''
303+
},
304+
description: nls.localize('schema.indentationRules', 'The language\'s indentation settings.'),
305+
type: 'object',
306+
properties: {
307+
increaseIndentPattern: {
308+
type: ['string', 'object'],
309+
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).'),
310+
properties: {
311+
pattern: {
312+
type: 'string',
313+
description: nls.localize('schema.indentationRules.increaseIndentPattern.pattern', 'The RegExp pattern for increaseIndentPattern.'),
314+
default: '',
315+
},
316+
flags: {
317+
type: 'string',
318+
description: nls.localize('schema.indentationRules.increaseIndentPattern.flags', 'The RegExp flags for increaseIndentPattern.'),
319+
default: '',
320+
pattern: '^([gimuy]+)$',
321+
patternErrorMessage: nls.localize('schema.indentationRules.increaseIndentPattern.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
322+
}
323+
}
324+
},
325+
decreaseIndentPattern: {
326+
type: ['string', 'object'],
327+
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).'),
328+
properties: {
329+
pattern: {
330+
type: 'string',
331+
description: nls.localize('schema.indentationRules.decreaseIndentPattern.pattern', 'The RegExp pattern for decreaseIndentPattern.'),
332+
default: '',
333+
},
334+
flags: {
335+
type: 'string',
336+
description: nls.localize('schema.indentationRules.decreaseIndentPattern.flags', 'The RegExp flags for decreaseIndentPattern.'),
337+
default: '',
338+
pattern: '^([gimuy]+)$',
339+
patternErrorMessage: nls.localize('schema.indentationRules.decreaseIndentPattern.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
340+
}
341+
}
342+
},
343+
indentNextLinePattern: {
344+
type: ['string', 'object'],
345+
description: nls.localize('schema.indentationRules.indentNextLinePattern', 'If a line matches this pattern, then **only the next line** after it should be indented once.'),
346+
properties: {
347+
pattern: {
348+
type: 'string',
349+
description: nls.localize('schema.indentationRules.indentNextLinePattern.pattern', 'The RegExp pattern for indentNextLinePattern.'),
350+
default: '',
351+
},
352+
flags: {
353+
type: 'string',
354+
description: nls.localize('schema.indentationRules.indentNextLinePattern.flags', 'The RegExp flags for indentNextLinePattern.'),
355+
default: '',
356+
pattern: '^([gimuy]+)$',
357+
patternErrorMessage: nls.localize('schema.indentationRules.indentNextLinePattern.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
358+
}
359+
}
360+
},
361+
unIndentedLinePattern: {
362+
type: ['string', 'object'],
363+
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.'),
364+
properties: {
365+
pattern: {
366+
type: 'string',
367+
description: nls.localize('schema.indentationRules.unIndentedLinePattern.pattern', 'The RegExp pattern for unIndentedLinePattern.'),
368+
default: '',
369+
},
370+
flags: {
371+
type: 'string',
372+
description: nls.localize('schema.indentationRules.unIndentedLinePattern.flags', 'The RegExp flags for unIndentedLinePattern.'),
373+
default: '',
374+
pattern: '^([gimuy]+)$',
375+
patternErrorMessage: nls.localize('schema.indentationRules.unIndentedLinePattern.errorMessage', 'Must match the pattern `/^([gimuy]+)$/`.')
376+
}
377+
}
378+
}
379+
}
255380
}
256381
}
257382
};

0 commit comments

Comments
 (0)