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 23, 2020
1 parent 2fc02f6 commit cb3bcad
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
9 changes: 1 addition & 8 deletions packages/editor/src/browser/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const TextEditorProvider = Symbol('TextEditorProvider');
export type TextEditorProvider = (uri: URI) => Promise<TextEditor>;

export interface TextEditorDocument extends lsp.TextDocument, Saveable, Disposable {
findMatches(options: FindMatchesOptions): FindMatch[];
getLineContent(lineNumber: number): string;
getLineMaxColumn(lineNumber: number): number;
}
Expand Down Expand Up @@ -277,14 +278,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
33 changes: 32 additions & 1 deletion 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 } 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 @@ -54,6 +54,7 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument {
protected bufferSavedVersionId: number;

protected model: monaco.editor.IModel;

protected readonly resolveModel: Promise<void>;

protected readonly toDispose = new DisposableCollection();
Expand Down Expand Up @@ -83,6 +84,7 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument {
protected readonly resource: Resource,
protected readonly m2p: MonacoToProtocolConverter,
protected readonly p2m: ProtocolToMonacoConverter,
protected readonly editorPreference: EditorPreferences,
protected readonly logger?: ILogger
) {
this.toDispose.push(resource);
Expand Down Expand Up @@ -279,6 +281,35 @@ 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 results: monaco.editor.FindMatch[] = this.model.findMatches(
options.searchString,
false,
options.isRegex,
options.matchCase,
// eslint-disable-next-line no-null/no-null
options.matchWholeWord ? this.editorPreference['editor.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.editorPreferences, this.logger);
}

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,7 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
* @returns the list of matches.
*/
protected findMatches(searchTerm: string, widget: EditorWidget, searchOptions: SearchInWorkspaceOptions): SearchMatch[] {
if (!widget.editor.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 cb3bcad

Please sign in to comment.