Skip to content

Commit

Permalink
refactor: improve selection bridge (#24)
Browse files Browse the repository at this point in the history
* refactor: improve selection bridge

Signed-off-by: Steven E Wright <StevenEWright@users.noreply.github.com>

* fix: address deepsource report

Signed-off-by: Steven E Wright <StevenEWright@users.noreply.github.com>

* fix: address remaining issues

Signed-off-by: Steven E Wright <StevenEWright@users.noreply.github.com>

---------

Signed-off-by: Steven E Wright <StevenEWright@users.noreply.github.com>
  • Loading branch information
StevenEWright committed Aug 15, 2023
1 parent e33a998 commit 24cc606
Show file tree
Hide file tree
Showing 19 changed files with 905 additions and 645 deletions.
67 changes: 39 additions & 28 deletions src/internals/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export class Editor
this.view = new TextEditorView(options.window, parent, this);
this.view.spellchecking = options.spellcheck;
this.view.dir = options.dir;
this.view.onLineMetricsChanged = metrics => {
if (this.gutter) {
this.gutter.setNumberOfLines(metrics.length);
}
};
this.view.language = options.language;
if (options.highlight) this.view.highlightElement = options.highlight;
}
Expand All @@ -120,6 +125,14 @@ export class Editor
this.view.spellchecking = spellcheck;
}

/** @inheritDoc */
get wrapsText(): boolean {
return this.view.wrapsText;
}
set wrapsText(wrapsText: boolean) {
this.view.wrapsText = wrapsText;
}

/** @inheritDoc */
get language(): string {
return this.view.language;
Expand Down Expand Up @@ -162,11 +175,20 @@ export class Editor
* @returns The current text content of the editor.
*/
get code(): string {
return this.view.cachedDocument.text;
return this.view.document.text;
}

set code(code: string) {
this.view.setDocumentUnchecked(new TextDocument(0, 0, code, true));
this.view.document = new TextDocument(0, 0, code, false); //.selectAll().collapseToEnd();
this.notifyPotentiallyChanged(true);
}

/** @inheritDoc */
get document(): TextDocument {
return this.view.document;
}
set document(document: TextDocument) {
this.view.document = document;
this.notifyPotentiallyChanged(true);
}

Expand All @@ -189,7 +211,7 @@ export class Editor

/** @inheritDoc */
currentUndoRedoState(): TextDocument {
return this.view.cachedDocument;
return this.view.document;
}

/** @inheritDoc */
Expand All @@ -200,15 +222,15 @@ export class Editor

/** @inheritDoc */
restoreUndoRedoState(document: TextDocument) {
this.view.setDocumentUnchecked(document);
this.view.document = document;
}

// ---------------- EditorInputEventHandler ----------------

/** @inheritDoc */
selectionChanged(): void {
if (this.options.onSelectionFocusChanged) {
this.options.onSelectionFocusChanged(this.view.cachedDocument);
this.options.onSelectionFocusChanged(this.view.document);
}
}

Expand All @@ -228,17 +250,16 @@ export class Editor
cut(event: ClipboardEvent): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const originalEvent = (event as any).originalEvent ?? event;
originalEvent.clipboardData.setData('text/plain', this.view.cachedDocument.selectedText);
this.view.setDocumentUnchecked(this.view.cachedDocument.deleteSelection());
originalEvent.clipboardData.setData('text/plain', this.view.document.selectedText);
this.view.document = this.view.document.deleteSelection();
}

/** @inheritDoc */
paste(event: ClipboardEvent): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const originalEvent = (event as any).originalEvent ?? event;
const text = originalEvent.clipboardData.getData('text/plain').replace(/\r\n/g, '\n');
// this.textEditor.type(text, true, true);
this.view.cachedDocument = this.view.cachedDocument.insertText(text, true, true);
this.view.document = this.view.document.insertText(text, true, true);
}

/** @inheritDoc */
Expand Down Expand Up @@ -271,7 +292,7 @@ export class Editor

private processPipeline(pipeline: InputProcessor[], args: InputProcessorArgs): boolean {
let handled = false;
const originalDocument = this.view.cachedDocument;
const originalDocument = this.view.document;
let document = originalDocument;
for (const processor of pipeline) {
const result = processor(document, args);
Expand All @@ -284,7 +305,7 @@ export class Editor
}
}
if (!document.strictEquals(originalDocument)) {
this.view.cachedDocument = document;
this.view.document = document;
}
return handled;
}
Expand All @@ -300,30 +321,20 @@ export class Editor
* @param programmatic Whether the change was made programmatically.
*/
private notifyPotentiallyChanged(programmatic: boolean = false) {
if (!this.undoRedoManager.dirty()) {
return;
}
if (this.gutter) {
const linesCount = this.code.replace(/\n+$/, '\n').split('\n').length;
this.gutter.setNumberOfLines(linesCount);
if (this.undoRedoManager.dirty()) {
if (programmatic) {
this.undoRedoManager.reset();
} else {
this.debouncedRecordHistory();
}
}
this.updateLineCounts();
if (programmatic) {
this.undoRedoManager.reset();
} else {
this.debouncedRecordHistory();

if (!programmatic) {
if (this.options.onUpdate) {
this.options.onUpdate(this.code);
}
}
}

/** Updates the line count in the gutter. */
private updateLineCounts(): void {
if (!this.gutter) return;
}

private readonly debouncedRecordHistory = debounce(() => {
this.undoRedoManager.push();
}, HISTORY_DEBOUNCE_MS);
Expand Down
11 changes: 0 additions & 11 deletions src/internals/gutter/gutter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */

import {Package} from '../../package';
import {GutterCustomizer} from './customizer';
import {GutterLineElement} from './line';
import {GutterOptions} from './options';
Expand Down Expand Up @@ -224,11 +223,6 @@ function createHighlightElement(opts: GutterOptions): HTMLElement {
* @returns The DOM element for the specified gutter options.
*/
function createGutterElement(opts: GutterOptions): HTMLElement {
if (Package.environment === 'DEVELOPMENT') {
// skipcq: JS-0002: Avoid console
console.log(' 🖼️ Creating New Gutter DOM Element');
}

const gutter = document.createElement('div');
gutter.className = opts.class ?? '';
gutter.dir = opts.dir;
Expand Down Expand Up @@ -258,11 +252,6 @@ function createGutterElement(opts: GutterOptions): HTMLElement {
* @returns The gutter line elements.
*/
function createGutterLineElement(): GutterLineElement {
if (Package.environment === 'DEVELOPMENT') {
// skipcq: JS-0002: Avoid console
console.log(' 🖼️ Creating New Gutter Line Element DOM Element');
}

const gutterLineWrapper = document.createElement('div');
gutterLineWrapper.style.display = 'block';
gutterLineWrapper.style.width = '100%';
Expand Down
6 changes: 0 additions & 6 deletions src/internals/pipeline/bracket_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */

import {InputProcessor, InputProcessorArgs} from './input_processor';
import {TextDocument} from '../text_editor';
import {Package} from '../../package';

/**
* A list of quotes. This is used to determine whether the user is typing a quote. All the
Expand Down Expand Up @@ -50,11 +49,6 @@ export function bracketProcessor(): InputProcessor {
* @returns Whether the processor handled the event.
*/
return (document: TextDocument, args: InputProcessorArgs): TextDocument | undefined => {
if (Package.environment === 'DEVELOPMENT') {
// skipcq: JS-0002: Avoid console
console.log(' ✍️ bracketProcessor');
}

const keyCode = args.event.keyCode;
const escapeCharacter = document.preceedingText.endsWith('\\');
const charAfter = document.followingText.slice(0, 1);
Expand Down
6 changes: 0 additions & 6 deletions src/internals/pipeline/newline_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */

import {InputProcessor, InputProcessorArgs} from './input_processor';
import {TextDocument} from '../text_editor';
import {Package} from '../../package';

/**
* # Newline Processor Options
Expand Down Expand Up @@ -100,11 +99,6 @@ export function newlineProcessor(opts: Partial<NewlineProcessorOptions>): InputP
if (!args.event.isEnter) return;
args.handled = true;

if (Package.environment === 'DEVELOPMENT') {
// skipcq: JS-0002: Avoid console
console.log(' ✍️ NewlineProcessor');
}

const before = document.preceedingText;
const after = document.followingText;
const currentLine = document.preceedingText.split('\n').pop() ?? document.preceedingText;
Expand Down
6 changes: 0 additions & 6 deletions src/internals/pipeline/tab_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */

import {InputProcessor, InputProcessorArgs} from './input_processor';
import {TextDocument} from '../text_editor';
import {Package} from '../../package';

/**
* # Tab Processor Options
Expand Down Expand Up @@ -62,11 +61,6 @@ export function tabProcessor(opts: Partial<TabProcessorOptions>): InputProcessor
if (!args.event.isTab) return;
args.handled = true;

if (Package.environment === 'DEVELOPMENT') {
// skipcq: JS-0002: Avoid console
console.log(' ✍️ TabProcessor');
}

const shifted = args.event.isShift;

const cursorMode = document.selectionType === 'caret';
Expand Down
5 changes: 4 additions & 1 deletion src/internals/text_editor/docs_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */
export {TextDocument} from './text_document';
export {type TextEditorViewKeyboardEvent as TextEditorViewKeyboardEvent} from './keyboard_event';
export {type TextEditorViewEventHandler} from './event_handler';
export {SelectionBridge} from './selection_bridge';
export {domToDocument} from './dom/dom_reader';
export {documentToDom} from './dom/dom_content_writer';
export {mutateSelectionInDom} from './dom/dom_selection_writer';
export {DomBridge} from './dom/dom_bridge';
export {TextEditorView} from './text_editor_view';
Loading

0 comments on commit 24cc606

Please sign in to comment.