|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { Editor as IAceEditor, Range as AceRange } from 'brace'; |
| 21 | +import { CoreEditor, Position, Range, Token, TokensProvider } from '../../types'; |
| 22 | +import { AceTokensProvider } from '../../lib/ace_token_provider'; |
| 23 | + |
| 24 | +export class LegacyEditor implements CoreEditor { |
| 25 | + constructor(private readonly editor: IAceEditor) {} |
| 26 | + |
| 27 | + getLineState(lineNumber: number) { |
| 28 | + const session = this.editor.getSession(); |
| 29 | + return session.getState(lineNumber - 1); |
| 30 | + } |
| 31 | + |
| 32 | + getValueInRange({ start, end }: Range): string { |
| 33 | + const session = this.editor.getSession(); |
| 34 | + const aceRange = new AceRange( |
| 35 | + start.lineNumber - 1, |
| 36 | + start.column - 1, |
| 37 | + end.lineNumber - 1, |
| 38 | + end.column - 1 |
| 39 | + ); |
| 40 | + return session.doc.getTextRange(aceRange); |
| 41 | + } |
| 42 | + |
| 43 | + getTokenProvider(): TokensProvider { |
| 44 | + return new AceTokensProvider(this.editor.getSession()); |
| 45 | + } |
| 46 | + |
| 47 | + getValue(): string { |
| 48 | + return this.editor.getValue(); |
| 49 | + } |
| 50 | + |
| 51 | + getLineValue(lineNumber: number): string { |
| 52 | + const session = this.editor.getSession(); |
| 53 | + return session.getLine(lineNumber - 1); |
| 54 | + } |
| 55 | + |
| 56 | + getCurrentPosition(): Position { |
| 57 | + const cursorPosition = this.editor.getCursorPosition(); |
| 58 | + return { |
| 59 | + lineNumber: cursorPosition.row + 1, |
| 60 | + column: cursorPosition.column + 1, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + clearSelection(): void { |
| 65 | + this.editor.clearSelection(); |
| 66 | + } |
| 67 | + |
| 68 | + getTokenAt(pos: Position): Token | null { |
| 69 | + const provider = this.getTokenProvider(); |
| 70 | + return provider.getTokenAt(pos); |
| 71 | + } |
| 72 | + |
| 73 | + insert(valueOrPos: string | Position, value?: string): void { |
| 74 | + if (typeof valueOrPos === 'string') { |
| 75 | + this.editor.insert(valueOrPos); |
| 76 | + return; |
| 77 | + } |
| 78 | + const document = this.editor.getSession().getDocument(); |
| 79 | + document.insert( |
| 80 | + { |
| 81 | + column: valueOrPos.column - 1, |
| 82 | + row: valueOrPos.lineNumber - 1, |
| 83 | + }, |
| 84 | + value || '' |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + moveCursorToPosition(pos: Position): void { |
| 89 | + this.editor.moveCursorToPosition({ row: pos.lineNumber - 1, column: pos.column - 1 }); |
| 90 | + } |
| 91 | + |
| 92 | + replace({ start, end }: Range, value: string): void { |
| 93 | + const aceRange = new AceRange( |
| 94 | + start.lineNumber - 1, |
| 95 | + start.column - 1, |
| 96 | + end.lineNumber - 1, |
| 97 | + end.column - 1 |
| 98 | + ); |
| 99 | + const session = this.editor.getSession(); |
| 100 | + session.replace(aceRange, value); |
| 101 | + } |
| 102 | + |
| 103 | + getLines(startLine: number, endLine: number): string[] { |
| 104 | + const session = this.editor.getSession(); |
| 105 | + return session.getLines(startLine - 1, endLine - 1); |
| 106 | + } |
| 107 | +} |
0 commit comments