Skip to content

Commit

Permalink
Use more generic word pattern for markdown
Browse files Browse the repository at this point in the history
Try to better support handle of unicode in markdown by using a word definition that use unicode character class

Fixes #15177
  • Loading branch information
mjbvz committed Dec 11, 2018
1 parent 0795547 commit 100adeb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions extensions/markdown-language-features/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export function activate(context: vscode.ExtensionContext) {
const previewManager = new MarkdownPreviewManager(contentProvider, logger, contributions);
context.subscriptions.push(previewManager);

context.subscriptions.push(vscode.languages.setLanguageConfiguration('markdown', {
wordPattern: /(\p{Alphabetic}|\p{Number})+/ug
}));
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(selector, symbolProvider));
context.subscriptions.push(vscode.languages.registerDocumentLinkProvider(selector, new LinkProvider()));
context.subscriptions.push(vscode.languages.registerFoldingRangeProvider(selector, new MarkdownFoldingProvider(engine)));
Expand Down
5 changes: 3 additions & 2 deletions src/vs/base/common/marshalling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { URI } from 'vs/base/common/uri';
import { regExpFlags } from 'vs/base/common/strings';

export function stringify(obj: any): string {
return JSON.stringify(obj, replacer);
Expand All @@ -24,8 +25,8 @@ function replacer(key: string, value: any): any {
if (value instanceof RegExp) {
return {
$mid: 2,
source: (<RegExp>value).source,
flags: ((<RegExp>value).global ? 'g' : '') + ((<RegExp>value).ignoreCase ? 'i' : '') + ((<RegExp>value).multiline ? 'm' : ''),
source: value.source,
flags: regExpFlags(value),
};
}
return value;
Expand Down
11 changes: 11 additions & 0 deletions src/vs/base/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export interface RegExpOptions {
wholeWord?: boolean;
multiline?: boolean;
global?: boolean;
unicode?: boolean;
}

export function createRegExp(searchString: string, isRegex: boolean, options: RegExpOptions = {}): RegExp {
Expand Down Expand Up @@ -215,6 +216,9 @@ export function createRegExp(searchString: string, isRegex: boolean, options: Re
if (options.multiline) {
modifiers += 'm';
}
if (options.unicode) {
modifiers += 'u';
}

return new RegExp(searchString, modifiers);
}
Expand All @@ -236,6 +240,13 @@ export function regExpContainsBackreference(regexpValue: string): boolean {
return !!regexpValue.match(/([^\\]|^)(\\\\)*\\\d+/);
}

export function regExpFlags(regexp: RegExp): string {
return (regexp.global ? 'g' : '')
+ (regexp.ignoreCase ? 'i' : '')
+ (regexp.multiline ? 'm' : '')
+ ((regexp as any).unicode ? 'u' : '');
}

/**
* Returns first index of the string that is not whitespace.
* If string is empty or contains only whitespaces, returns -1
Expand Down
3 changes: 3 additions & 0 deletions src/vs/editor/common/model/wordHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export function ensureValidWordDefinition(wordDefinition?: RegExp | null): RegEx
if (wordDefinition.multiline) {
flags += 'm';
}
if ((wordDefinition as any).unicode) {
flags += 'u';
}
result = new RegExp(wordDefinition.source, flags);
} else {
result = wordDefinition;
Expand Down
7 changes: 4 additions & 3 deletions src/vs/editor/common/services/editorWorkerServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { EditorSimpleWorkerImpl } from 'vs/editor/common/services/editorSimpleWo
import { IDiffComputationResult, IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
import { IModelService } from 'vs/editor/common/services/modelService';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
import { regExpFlags } from 'vs/base/common/strings';

/**
* Stop syncing a model to the worker if it was not needed for 1 min.
Expand Down Expand Up @@ -413,7 +414,7 @@ export class EditorWorkerClient extends Disposable {
}
let wordDefRegExp = LanguageConfigurationRegistry.getWordDefinition(model.getLanguageIdentifier().id);
let wordDef = wordDefRegExp.source;
let wordDefFlags = (wordDefRegExp.global ? 'g' : '') + (wordDefRegExp.ignoreCase ? 'i' : '') + (wordDefRegExp.multiline ? 'm' : '');
let wordDefFlags = regExpFlags(wordDefRegExp);
return proxy.textualSuggest(resource.toString(), position, wordDef, wordDefFlags);
});
}
Expand All @@ -426,7 +427,7 @@ export class EditorWorkerClient extends Disposable {
}
let wordDefRegExp = LanguageConfigurationRegistry.getWordDefinition(model.getLanguageIdentifier().id);
let wordDef = wordDefRegExp.source;
let wordDefFlags = (wordDefRegExp.global ? 'g' : '') + (wordDefRegExp.ignoreCase ? 'i' : '') + (wordDefRegExp.multiline ? 'm' : '');
let wordDefFlags = regExpFlags(wordDefRegExp);
return proxy.computeWordRanges(resource.toString(), range, wordDef, wordDefFlags);
});
}
Expand All @@ -439,7 +440,7 @@ export class EditorWorkerClient extends Disposable {
}
let wordDefRegExp = LanguageConfigurationRegistry.getWordDefinition(model.getLanguageIdentifier().id);
let wordDef = wordDefRegExp.source;
let wordDefFlags = (wordDefRegExp.global ? 'g' : '') + (wordDefRegExp.ignoreCase ? 'i' : '') + (wordDefRegExp.multiline ? 'm' : '');
let wordDefFlags = regExpFlags(wordDefRegExp);
return proxy.navigateValueSet(resource.toString(), range, up, wordDef, wordDefFlags);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/node/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ExtHostCommands, CommandsConverter } from 'vs/workbench/api/node/extHos
import { ExtHostDiagnostics } from 'vs/workbench/api/node/extHostDiagnostics';
import { asThenable } from 'vs/base/common/async';
import { MainContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape, ObjectIdentifier, IRawColorInfo, IMainContext, IdObject, ISerializedRegExp, ISerializedIndentationRule, ISerializedOnEnterRule, ISerializedLanguageConfiguration, WorkspaceSymbolDto, SuggestResultDto, WorkspaceSymbolsDto, SuggestionDto, CodeActionDto, ISerializedDocumentFilter, WorkspaceEditDto, ISerializedSignatureHelpProviderMetadata } from './extHost.protocol';
import { regExpLeadsToEndlessLoop } from 'vs/base/common/strings';
import { regExpLeadsToEndlessLoop, regExpFlags } from 'vs/base/common/strings';
import { IPosition } from 'vs/editor/common/core/position';
import { IRange, Range as EditorRange } from 'vs/editor/common/core/range';
import { isFalsyOrEmpty, isNonEmptyArray } from 'vs/base/common/arrays';
Expand Down Expand Up @@ -1299,7 +1299,7 @@ export class ExtHostLanguageFeatures implements ExtHostLanguageFeaturesShape {
}
return {
pattern: regExp.source,
flags: (regExp.global ? 'g' : '') + (regExp.ignoreCase ? 'i' : '') + (regExp.multiline ? 'm' : ''),
flags: regExpFlags(regExp),
};
}

Expand Down

0 comments on commit 100adeb

Please sign in to comment.