Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add types for vim.js #162

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
"@codemirror/view": "^6.0.3"
},
"devDependencies": {
"codemirror": "6.0.0",
"@codemirror/buildhelper": "^0.1.16",
"@codemirror/lang-javascript": "^6.0.0",
"@codemirror/lang-xml": "^6.0.0",
"@codemirror/language": "^6.1.0",
"codemirror": "6.0.0",
"typescript": "^5.0.2",
"vite": "^2.9.6"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/block-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class BlockCursorPlugin {

readPos(): Measure {
let {state} = this.view
let cursors = []
let cursors: Piece[] = []
for (let r of state.selection.ranges) {
let prim = r == state.selection.main
let piece = measureCursor(this.cm, this.view, r, prim)
Expand Down
64 changes: 38 additions & 26 deletions src/cm_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { StringStream, matchBrackets, indentUnit, ensureSyntaxTree, foldCode } f
import { EditorView, runScopeHandlers, ViewUpdate } from "@codemirror/view"
import { RegExpCursor, setSearchQuery, SearchQuery } from "@codemirror/search"
import {
insertNewlineAndIndent, indentMore, indentLess, indentSelection,
deleteCharBackward, deleteCharForward, cursorCharLeft,
insertNewlineAndIndent, indentMore, indentLess, indentSelection, cursorCharLeft,
undo, redo, cursorLineBoundaryBackward, cursorLineBoundaryForward, cursorCharBackward,
} from "@codemirror/commands"
import {vimState, CM5RangeInterface} from "./types"

interface Pos { line: number, ch: number }
interface CM5Range { anchor: Pos, head: Pos }
function indexFromPos(doc: Text, pos: Pos): number {
var ch = pos.ch;
var lineNumber = pos.line + 1;
Expand All @@ -29,6 +27,9 @@ function posFromIndex(doc: Text, offset: number): Pos {
return { line: line.number - 1, ch: offset - line.from }
}
class Pos {
line: number
ch: number
sticky?: string
constructor(line: number, ch: number) {
this.line = line; this.ch = ch;
}
Expand Down Expand Up @@ -121,14 +122,14 @@ function runHistoryCommand(cm: CodeMirror, revert: boolean) {

var keys: Record<string, (cm: CodeMirror) => void> = {};
"Left|Right|Up|Down|Backspace|Delete".split("|").forEach(key => {
keys[key] = (cm:CodeMirror) => runScopeHandlers(cm.cm6, {key: key}, "editor");
keys[key] = (cm:CodeMirror) => runScopeHandlers(cm.cm6, {key: key} as KeyboardEvent, "editor");
});

export class CodeMirror {
static isMac = typeof navigator != "undefined" && /Mac/.test(navigator.platform);
// --------------------------
static Pos = Pos;
static StringStream = StringStream;
static StringStream = StringStream as unknown as StringStream & { new(_: string): StringStream }
static commands = {
cursorCharLeft: function (cm: CodeMirror) { cursorCharLeft(cm.cm6); },
redo: function (cm: CodeMirror) { runHistoryCommand(cm, false); },
Expand All @@ -143,17 +144,16 @@ export class CodeMirror {
},
indentAuto: function (cm: CodeMirror) {
indentSelection(cm.cm6)
}
},
newlineAndIndentContinueComment: undefined as any,
save: undefined as any,
};
static defineOption = function (name: string, val: any, setter: Function) { };
static isWordChar = function (ch: string) {
return wordChar.test(ch);
};
static keys: any = keys;
static keyMap = {
};
static addClass = function () { };
static rmClass = function () { };
static addClass = function (el, str) { };
static rmClass = function (el, str) { };
static e_preventDefault = function (e: Event) {
e.preventDefault()
};
Expand Down Expand Up @@ -188,10 +188,11 @@ export class CodeMirror {
statusbar?: Element | null,
dialog?: Element | null,
vimPlugin?: any,
vim?: any,
vim?: vimState | null,
currentNotificationClose?: Function | null,
keyMap?: string,
overwrite?: boolean,
textwidth?: number,
} = {};
marks: Record<string, Marker> = Object.create(null);
$mid = 0; // marker id counter
Expand Down Expand Up @@ -231,12 +232,14 @@ export class CodeMirror {
firstLine() { return 0; };
lastLine() { return this.cm6.state.doc.lines - 1; };
lineCount() { return this.cm6.state.doc.lines };
setCursor(line: Pos | number, ch: number) {
setCursor(line: number, ch: number): void;
setCursor(line: Pos): void;
setCursor(line: Pos | number, ch?: number) {
if (typeof line === 'object') {
ch = line.ch;
line = line.line;
}
var offset = indexFromPos(this.cm6.state.doc, { line, ch })
var offset = indexFromPos(this.cm6.state.doc, { line, ch: ch || 0 })
this.cm6.dispatch({ selection: { anchor: offset } }, { scrollIntoView: !this.curOp })
if (this.curOp && !this.curOp.isVimOp)
this.onBeforeEndOperation();
Expand Down Expand Up @@ -266,7 +269,7 @@ export class CodeMirror {
};
});
};
setSelections(p: CM5Range[], primIndex?: number) {
setSelections(p: CM5RangeInterface[], primIndex?: number) {
var doc = this.cm6.state.doc
var ranges = p.map(x => {
return EditorSelection.range(indexFromPos(doc, x.anchor), indexFromPos(doc, x.head))
Expand Down Expand Up @@ -316,7 +319,7 @@ export class CodeMirror {
indexFromPos(doc, e)
)
};
replaceRange(text: string, s: Pos, e: Pos) {
replaceRange(text: string, s: Pos, e?: Pos, source?: string) {
if (!e) e = s;
var doc = this.cm6.state.doc;
var from = indexFromPos(doc, s);
Expand Down Expand Up @@ -386,7 +389,7 @@ export class CodeMirror {
return this.cm6.defaultLineHeight
};

findMatchingBracket(pos: Pos) {
findMatchingBracket(pos: Pos, _options?: any) {
var state = this.cm6.state
var offset = indexFromPos(state.doc, pos);
var m = matchBrackets(state, offset + 1, -1)
Expand All @@ -403,7 +406,7 @@ export class CodeMirror {
return scanForBracket(this, pos, dir, style, config);
};

indentLine(line: number, more: boolean) {
indentLine(line: number, more?: boolean) {
// todo how to indent only one line instead of selection
if (more) this.indentMore()
else this.indentLess()
Expand Down Expand Up @@ -547,7 +550,7 @@ export class CodeMirror {
}
}

let pos = posFromIndex(doc, range.head) as Pos&{hitSide: boolean};
let pos = posFromIndex(doc, range.head) as Pos&{hitSide?: boolean};
// set hitside to true if there was no place to move and cursor was clipped to the edge
// of document. Needed for gj/gk
if (
Expand Down Expand Up @@ -588,7 +591,7 @@ export class CodeMirror {
clientHeight: scroller.clientHeight, clientWidth: scroller.clientWidth
};
};
scrollTo(x?: number, y?: number) {
scrollTo(x?: number|null, y?: number|null) {
if (x != null)
this.cm6.scrollDOM.scrollLeft = x
if (y != null)
Expand Down Expand Up @@ -664,7 +667,7 @@ export class CodeMirror {
curOp.cursorActivityHandlers = this._handlers["cursorActivity"] && this._handlers["cursorActivity"].slice();
this.curOp.cursorActivity = true;
};
operation(fn: Function) {
operation(fn: Function, force?: boolean) {
if (!this.curOp)
this.curOp = { $d: 0 };
this.curOp.$d++;
Expand Down Expand Up @@ -717,6 +720,8 @@ export class CodeMirror {

}
};
getOption(name:"firstLineNumber"|"tabSize"): number;
getOption(name:string): number|boolean|string|undefined;
getOption(name: string) {
switch (name) {
case "firstLineNumber": return 1;
Expand Down Expand Up @@ -767,7 +772,7 @@ export class CodeMirror {
virtualSelectionMode() {
return !!this.virtualSelection
}
virtualSelection: EditorSelection | null = null;
virtualSelection: Mutable<EditorSelection> | null = null;
forEachSelection(command: Function) {
var selection = this.cm6.state.selection;
this.virtualSelection = EditorSelection.create(selection.ranges, selection.mainIndex)
Expand All @@ -785,8 +790,15 @@ export class CodeMirror {
hardWrap(options) {
return hardWrap(this, options);
}

showMatchesOnScrollbar?: Function // not implemented
save?: Function
static keyName?: Function = undefined
};

type Mutable<Type> = {
-readonly [Key in keyof Type]: Type[Key];
};

/************* dialog *************/

Expand Down Expand Up @@ -924,7 +936,7 @@ function scanForBracket(cm: CodeMirror, where: Pos, dir: -1 | 1, style: any, con
var maxScanLen = (config && config.maxScanLineLength) || 10000;
var maxScanLines = (config && config.maxScanLines) || 1000;

var stack = [];
var stack: string[] = [];
var re = bracketRegex(config)
var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
: Math.max(cm.firstLine() - 1, where.line - maxScanLines);
Expand All @@ -948,7 +960,7 @@ function scanForBracket(cm: CodeMirror, where: Pos, dir: -1 | 1, style: any, con
return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
}

function findMatchingTag(cm: CodeMirror, pos: Pos) {
function findMatchingTag(cm: CodeMirror, pos: Pos): undefined {
}

function findEnclosingTag(cm: CodeMirror, pos: Pos) {
Expand Down Expand Up @@ -1021,7 +1033,7 @@ function hardWrap(cm, options) {
if (line.length > max) {
var space = findSpace(line, max, 5);
if (space) {
var indentation = /^\s*/.exec(line)[0];
var indentation = /^\s*/.exec(line)?.[0];
cm.replaceRange("\n" + indentation, new Pos(row, space.start), new Pos(row, space.end));
}
endRow++;
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const vimPlugin = ViewPlugin.fromClass(
this.updateStatus();
});
this.cm.on("vim-mode-change", (e: any) => {
if (!cm.state.vim) return;
cm.state.vim.mode = e.mode;
if (e.subMode) {
cm.state.vim.mode += " block";
Expand Down Expand Up @@ -226,7 +227,7 @@ const vimPlugin = ViewPlugin.fromClass(

vim.status = (vim.status || "") + key;
let result = Vim.multiSelectHandleKey(cm, key, "user");
vim = cm.state.vim; // the object can change if there is an exception in handleKey
vim = Vim.maybeInitVimState_(cm); // the object can change if there is an exception in handleKey

// insert mode
if (!result && vim.insertMode && cm.state.overwrite) {
Expand Down
Loading
Loading