Skip to content

Commit

Permalink
monaco, siw: Move findMatches function into TextEditorDocument
Browse files Browse the repository at this point in the history
Signed-off-by: DukeNgn <[email protected]>
  • Loading branch information
DucNgn committed Oct 26, 2020
1 parent d72e1b2 commit 9de6563
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Change Log

## v1.7.0

- [editor] add `findMatches` method to `TextEditorDocument` interface
- [git] the changes in the commit details (opened from the history view) and in the diff view (opened with 'Compare With...' on a folder's context menu) are now switchable between 'list' and 'tree' modes [#8084](https://github.com/eclipse-theia/theia/pull/8084)
- [scm] show in the commit textbox the branch to which the commit will go [#6156](https://github.com/eclipse-theia/theia/pull/6156)
- [filesystem] file watchers refactoring:
Expand Down
4 changes: 3 additions & 1 deletion packages/editor/src/browser/editor-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const EDITOR_MODEL_DEFAULTS = {
largeFileOptimizations: true
};

export const USUAL_WORD_SEPARATORS = '`~!@#$%^&*()-=+[{]}\\|;:\'",.<>/?';

/* eslint-disable max-len */
/* eslint-disable no-null/no-null */

Expand Down Expand Up @@ -1171,7 +1173,7 @@ const codeEditorPreferenceProperties = {
'editor.wordSeparators': {
'description': 'Characters that will be used as word separators when doing word related navigations or operations.',
'type': 'string',
'default': '`~!@#$%^&*()-=+[{]}\\|;:\'",.<>/?'
'default': USUAL_WORD_SEPARATORS
},
'editor.wordWrap': {
'markdownEnumDescriptions': [
Expand Down
12 changes: 4 additions & 8 deletions packages/editor/src/browser/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export type TextEditorProvider = (uri: URI) => Promise<TextEditor>;
export interface TextEditorDocument extends lsp.TextDocument, Saveable, Disposable {
getLineContent(lineNumber: number): string;
getLineMaxColumn(lineNumber: number): number;
/**
* @since 1.7.0
*/
findMatches?(options: FindMatchesOptions): FindMatch[];
}

// Refactoring
Expand Down Expand Up @@ -277,14 +281,6 @@ export interface TextEditor extends Disposable, TextEditorSelection, Navigatable
setEncoding(encoding: string, mode: EncodingMode): void;

readonly onEncodingChanged: Event<string>;

/**
* Find all matches in an editor for the given options.
* @param options the options for finding matches.
*
* @returns the list of matches.
*/
findMatches?(options: FindMatchesOptions): FindMatch[];
}

export interface Dimension {
Expand Down
35 changes: 33 additions & 2 deletions packages/monaco/src/browser/monaco-editor-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { Position } from 'vscode-languageserver-types';
import { TextDocumentSaveReason, TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol';
import { TextEditorDocument, EncodingMode } from '@theia/editor/lib/browser';
import { TextEditorDocument, EncodingMode, FindMatchesOptions, FindMatch, EditorPreferences, USUAL_WORD_SEPARATORS } from '@theia/editor/lib/browser';
import { DisposableCollection, Disposable } from '@theia/core/lib/common/disposable';
import { Emitter, Event } from '@theia/core/lib/common/event';
import { CancellationTokenSource, CancellationToken } from '@theia/core/lib/common/cancellation';
Expand Down Expand Up @@ -83,7 +83,8 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument {
protected readonly resource: Resource,
protected readonly m2p: MonacoToProtocolConverter,
protected readonly p2m: ProtocolToMonacoConverter,
protected readonly logger?: ILogger
protected readonly logger?: ILogger,
protected readonly editorPreferences?: EditorPreferences
) {
this.toDispose.push(resource);
this.toDispose.push(this.toDisposeOnAutoSave);
Expand Down Expand Up @@ -279,6 +280,36 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument {
return this.model;
}

/**
* Find all matches in an editor for the given options.
* @param options the options for finding matches.
*
* @returns the list of matches.
*/
findMatches(options: FindMatchesOptions): FindMatch[] {
const wordSeparators = this.editorPreferences ? this.editorPreferences['editor.wordSeparators'] : USUAL_WORD_SEPARATORS;
const results: monaco.editor.FindMatch[] = this.model.findMatches(
options.searchString,
false,
options.isRegex,
options.matchCase,
// eslint-disable-next-line no-null/no-null
options.matchWholeWord ? wordSeparators : null,
true,
options.limitResultCount
);
const extractedMatches: FindMatch[] = [];
results.forEach(r => {
if (r.matches) {
extractedMatches.push({
matches: r.matches,
range: Range.create(r.range.startLineNumber, r.range.startColumn, r.range.endLineNumber, r.range.endColumn)
});
}
});
return extractedMatches;
}

async load(): Promise<MonacoEditorModel> {
await this.resolveModel;
return this;
Expand Down
30 changes: 1 addition & 29 deletions packages/monaco/src/browser/monaco-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
ReplaceTextParams,
EditorDecoration,
EditorMouseEvent,
EncodingMode, FindMatchesOptions, FindMatch
EncodingMode
} from '@theia/editor/lib/browser';
import { MonacoEditorModel } from './monaco-editor-model';
import { MonacoToProtocolConverter } from './monaco-to-protocol-converter';
Expand All @@ -48,7 +48,6 @@ import IEditorOverrideServices = monaco.editor.IEditorOverrideServices;
import IStandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
import IIdentifiedSingleEditOperation = monaco.editor.IIdentifiedSingleEditOperation;
import IBoxSizing = ElementExt.IBoxSizing;
import EditorOptions = monaco.editor.EditorOption;

@injectable()
export class MonacoEditorServices {
Expand Down Expand Up @@ -122,33 +121,6 @@ export class MonacoEditor extends MonacoEditorServices implements TextEditor {
return this.document.setEncoding(encoding, mode);
}

findMatches(options: FindMatchesOptions): FindMatch[] {
const model = this.editor.getModel();
if (!model) {
return [];
}
const results: monaco.editor.FindMatch[] = model.findMatches(
options.searchString,
false,
options.isRegex,
options.matchCase,
// eslint-disable-next-line no-null/no-null
options.matchWholeWord ? this.editor.getOption(EditorOptions.wordSeparators) : null,
true,
options.limitResultCount
);
const extractedMatches: FindMatch[] = [];
results.forEach(r => {
if (r.matches) {
extractedMatches.push({
matches: r.matches,
range: Range.create(r.range.startLineNumber, r.range.startColumn, r.range.endLineNumber, r.range.endColumn)
});
}
});
return extractedMatches;
}

protected create(options?: IStandaloneEditorConstructionOptions, override?: monaco.editor.IEditorOverrideServices): Disposable {
return this.editor = monaco.editor.create(this.node, {
...options,
Expand Down
2 changes: 1 addition & 1 deletion packages/monaco/src/browser/monaco-text-model-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class MonacoTextModelService implements monaco.editor.ITextModelService {

protected createModel(resource: Resource): MaybePromise<MonacoEditorModel> {
const factory = this.factories.getContributions().find(({ scheme }) => resource.uri.scheme === scheme);
return factory ? factory.createModel(resource) : new MonacoEditorModel(resource, this.m2p, this.p2m, this.logger);
return factory ? factory.createModel(resource) : new MonacoEditorModel(resource, this.m2p, this.p2m, this.logger, this.editorPreferences);
}

protected readonly modelOptions: { [name: string]: (keyof monaco.editor.ITextModelUpdateOptions | undefined) } = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,10 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
* @returns the list of matches.
*/
protected findMatches(searchTerm: string, widget: EditorWidget, searchOptions: SearchInWorkspaceOptions): SearchMatch[] {
if (!widget.editor.findMatches) {
if (!widget.editor.document.findMatches) {
return [];
}

const results: FindMatch[] = widget.editor.findMatches({
const results: FindMatch[] = widget.editor.document.findMatches({
searchString: searchTerm,
isRegex: !!searchOptions.useRegExp,
matchCase: !!searchOptions.matchCase,
Expand Down

0 comments on commit 9de6563

Please sign in to comment.