Skip to content

Commit

Permalink
refactor: move functions to more logical places
Browse files Browse the repository at this point in the history
  • Loading branch information
Fevol committed Mar 9, 2023
1 parent 40f0319 commit 9a3dd1a
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 138 deletions.
45 changes: 1 addition & 44 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,5 @@
import type {PluginSettings} from "./types";
import type { PluginSettings } from './types';

export const CM_Syntax: {[key: string]: [string, string]} = {
"Addition": ["+", "+"],
"Deletion": ["-", "-"],
"Substitution": ["~", "~"],
"Highlight": ["=", "="],
"Comment": [">", "<"]
}

export const CM_All_Brackets: {[key: string]: string[]} = {
"Addition": ["{++", "++}"],
"Deletion":["{--", "--}"],
"Substitution": ["{~~", "~>", "~~}"],
"Highlight": ["{==", "==}"],
"Comment": ["{>>", "<<}"]
}

export const CM_Brackets: {[key: string]: string[]} = {
"{++": ["++}"],
"{--": ["--}"],
"{~~": ["~>", "~~}"],
"{==": ["==}"],
"{>>": ["<<}"]
}


export function replaceBracket(content: string, type: string) {
return wrapBracket(unwrapBracket(content), type);
}

export function unwrapBracket(content: string) {
return content.slice(3, -3);
}

export function wrapBracket(content: string, type: string) {
return CM_All_Brackets[type][0] + content + CM_All_Brackets[type].slice(1).join('');
}

export function addBracket(content: string, type: string, left: boolean) {
if (left)
return '{' + CM_Syntax[type][0].repeat(2) + content;
else
return content + CM_Syntax[type][1].repeat(2) + '}';
}

export const DEFAULT_SETTINGS: PluginSettings = {
suggestion_status: 0,
Expand Down
6 changes: 3 additions & 3 deletions src/editor/commands.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { CommandI } from '../../types';
import type { Editor, EditorChange, EditorTransaction, MarkdownView } from 'obsidian';
import type { Editor, MarkdownView } from 'obsidian';

import type { Tree } from '@lezer/common';

import { criticmarkupLanguage } from './parser';
import { addBracket, unwrapBracket, wrapBracket } from '../constants';
import { ltEP, minEP, maxEP, nodesInSelection, selectionToRange } from './util';
import { ltEP, minEP, maxEP, nodesInSelection, selectionToRange } from './editor-util';
import type { ChangeSpec } from '@codemirror/state';
import { EditorSelection } from '@codemirror/state';
import { addBracket, unwrapBracket, wrapBracket } from '../util';


function changeSelectionType(editor: Editor, view: MarkdownView, type: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/context-menu-commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EventRef } from 'obsidian';
import { acceptAllSuggestions, rejectAllSuggestions } from './commands';
import { selectionToRange } from './util';
import { selectionToRange } from './editor-util';


export const change_suggestions: EventRef =
Expand Down
2 changes: 1 addition & 1 deletion src/editor/criticmarkup-gutter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EditorView, gutter, GutterMarker, PluginValue, ViewPlugin } from '@code
import { RangeSet, RangeSetBuilder } from '@codemirror/state';
import { Menu } from 'obsidian';
import { acceptAllSuggestions, rejectAllSuggestions } from './commands';
import { nodesInSelection } from './util';
import { nodesInSelection } from './editor-util';

export class CriticMarkupMarker extends GutterMarker {
constructor(readonly from: number, readonly to: number, readonly type: string, readonly top?: boolean, readonly bottom?: boolean) {
Expand Down
4 changes: 2 additions & 2 deletions src/editor/editor-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChangeSpec, EditorSelection, EditorState, Prec } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
import { CM_Brackets } from '../constants';
import { criticmarkupLanguage } from './parser';
import { moveEditorCursor, nodeAtCursor } from './util';
import { moveEditorCursor, nodeAtCursor } from './editor-util';
import { CM_Brackets } from '../util';

export const bracketMatcher = Prec.high(EditorView.inputHandler.of((view, from, to, text) => {
const before = view.state.doc.sliceString(from - 2, from) + text;
Expand Down
69 changes: 14 additions & 55 deletions src/editor/util.ts → src/editor/editor-util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { EditorPosition } from 'obsidian';
import type { Editor, EditorPosition } from 'obsidian';
import type { Tree } from '@lezer/common';
import { EditorSelection } from '@codemirror/state';
import type { Editor } from 'obsidian';


export function eqEP(a: EditorPosition, b: EditorPosition): boolean {
Expand All @@ -28,6 +27,16 @@ export function maxEP(a: EditorPosition, b: EditorPosition): EditorPosition {
return ltEP(a, b) ? b : a;
}

export function moveEditorCursor(selection: EditorSelection, change_start: number, offset: number) {
if (change_start >= selection.ranges[0].from)
return selection;
return EditorSelection.range(
selection.ranges[0].from + offset,
selection.ranges[0].to + offset,
)
}



export function selectionToRange(editor: Editor): number[] {
const selection = editor.listSelections()[0];
Expand All @@ -43,6 +52,8 @@ export function selectionRangeOverlap(selection: EditorSelection, rangeFrom: num
}




export function nodeAtCursor(tree: Tree, pos: number) {
const node = tree.resolve(pos, -1);
if (node.type.name === '⚠' || node.type.name === 'CriticMarkup')
Expand All @@ -52,14 +63,6 @@ export function nodeAtCursor(tree: Tree, pos: number) {
return node;
}

export function moveEditorCursor(selection: EditorSelection, change_start: number, offset: number) {
if (change_start >= selection.ranges[0].from)
return selection;
return EditorSelection.range(
selection.ranges[0].from + offset,
selection.ranges[0].to + offset,
)
}


export function nodesInSelection(tree: Tree, start?: number, end?: number) {
Expand Down Expand Up @@ -92,48 +95,4 @@ export function nodesInSelection(tree: Tree, start?: number, end?: number) {
},
});
return nodes;
}

export function objectDifference(new_obj: any, old_obj: any): Partial<typeof new_obj> {
const diff: Partial<typeof new_obj> = {};
for (const key in new_obj) {
if (new_obj[key] !== old_obj[key])
diff[key] = new_obj[key];
}
return diff;
}


// function nodesInText(tree: Tree) {
// const nodes: { from: number, middle?: number, to: number, type: string }[] = [];
//
// const cursor = tree.cursor();
// while (cursor.next()) {
// const start = cursor.from;
// const end = cursor.to;
// const name = cursor.name;
//
// // If error detected: return only the confirmed nodes (errored node will always contain all text after it, invalid)
// if (name === '⚠')
// return nodes.slice(0, -1);
//
// if (name === 'Substitution') {
// cursor.firstChild();
// if (cursor.name !== 'MSub') continue;
//
// nodes.push({
// from: start,
// middle: cursor.from,
// to: end,
// type: name,
// });
// } else {
// nodes.push({
// from: start,
// to: end,
// type: name,
// });
// }
// }
// return nodes;
// }
}
29 changes: 0 additions & 29 deletions src/editor/file-menu-commands.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/editor/live-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { TreeFragment } from '@lezer/common';
import { RangeSet } from '@codemirror/state';
import { buildMarkers, CriticMarkupMarker, gutterExtension } from './criticmarkup-gutter';
import type { PluginSettings } from '../types';
import { selectionRangeOverlap } from './util';
import { selectionRangeOverlap } from './editor-util';

export function inlinePlugin(settings: PluginSettings): Extension[] {
const view_plugin = ViewPlugin.fromClass(
Expand Down
2 changes: 1 addition & 1 deletion src/editor/post-processor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { criticmarkupLanguage } from './parser';

import { CM_Syntax } from '../constants';
import type {MarkdownView} from "obsidian";
import { CM_Syntax } from '../util';

export function postProcess(el: HTMLElement, ctx: any, settings: any) {
const tree = criticmarkupLanguage.parser.parse(el.innerHTML);
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { bracketMatcher, nodeCorrecter } from './editor/editor-handlers';
import type {PluginSettings} from "./types";
import {DEFAULT_SETTINGS} from "./constants";
import {loadEditorButtons, removeEditorButtons} from "./editor/editor-preview-buttons";
import {objectDifference} from "./editor/util";
import {CommentatorSettings} from "./ui/settings";
import { objectDifference } from './util';

export default class CommentatorPlugin extends Plugin {
private editorExtensions: Extension[] = [];
Expand Down
49 changes: 49 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export function objectDifference(new_obj: any, old_obj: any): Partial<typeof new_obj> {
const diff: Partial<typeof new_obj> = {};
for (const key in new_obj) {
if (new_obj[key] !== old_obj[key])
diff[key] = new_obj[key];
}
return diff;
}

export const CM_Syntax: { [key: string]: [string, string] } = {
'Addition': ['+', '+'],
'Deletion': ['-', '-'],
'Substitution': ['~', '~'],
'Highlight': ['=', '='],
'Comment': ['>', '<'],
};
export const CM_All_Brackets: { [key: string]: string[] } = {
'Addition': ['{++', '++}'],
'Deletion': ['{--', '--}'],
'Substitution': ['{~~', '~>', '~~}'],
'Highlight': ['{==', '==}'],
'Comment': ['{>>', '<<}'],
};
export const CM_Brackets: { [key: string]: string[] } = {
'{++': ['++}'],
'{--': ['--}'],
'{~~': ['~>', '~~}'],
'{==': ['==}'],
'{>>': ['<<}'],
};

export function replaceBracket(content: string, type: string) {
return wrapBracket(unwrapBracket(content), type);
}

export function unwrapBracket(content: string) {
return content.slice(3, -3);
}

export function wrapBracket(content: string, type: string) {
return CM_All_Brackets[type][0] + content + CM_All_Brackets[type].slice(1).join('');
}

export function addBracket(content: string, type: string, left: boolean) {
if (left)
return '{' + CM_Syntax[type][0].repeat(2) + content;
else
return content + CM_Syntax[type][1].repeat(2) + '}';
}

0 comments on commit 9a3dd1a

Please sign in to comment.