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 09f08cd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 43 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
34 changes: 33 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 } 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 All @@ -26,6 +26,7 @@ import { Saveable, SaveOptions } from '@theia/core/lib/browser/saveable';
import { MonacoToProtocolConverter } from './monaco-to-protocol-converter';
import { ProtocolToMonacoConverter } from './protocol-to-monaco-converter';
import { ILogger, Loggable, Log } from '@theia/core/lib/common/logger';
import EditorOptions = monaco.editor.EditorOption;

export {
TextDocumentSaveReason
Expand Down Expand Up @@ -54,6 +55,8 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument {
protected bufferSavedVersionId: number;

protected model: monaco.editor.IModel;
protected editor: monaco.editor.IStandaloneCodeEditor;

protected readonly resolveModel: Promise<void>;

protected readonly toDispose = new DisposableCollection();
Expand Down Expand Up @@ -279,6 +282,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.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;
}

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
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 09f08cd

Please sign in to comment.