Skip to content

Commit

Permalink
add more types
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing committed Feb 4, 2024
1 parent 2af6884 commit f390c93
Show file tree
Hide file tree
Showing 3 changed files with 303 additions and 78 deletions.
30 changes: 19 additions & 11 deletions src/cm_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
insertNewlineAndIndent, indentMore, indentLess, indentSelection, cursorCharLeft,
undo, redo, cursorLineBoundaryBackward, cursorLineBoundaryForward, cursorCharBackward,
} from "@codemirror/commands"
import {vimState, CM5Range} from "./types"
import {vimState, CM5RangeInterface} from "./types"

function indexFromPos(doc: Text, pos: Pos): number {
var ch = pos.ch;
Expand All @@ -29,6 +29,7 @@ function posFromIndex(doc: Text, offset: number): Pos {
class Pos {
line: number
ch: number
sticky?: string
constructor(line: number, ch: number) {
this.line = line; this.ch = ch;
}
Expand Down Expand Up @@ -128,7 +129,7 @@ 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,7 +144,8 @@ export class CodeMirror {
},
indentAuto: function (cm: CodeMirror) {
indentSelection(cm.cm6)
}
},
newlineAndIndentContinueComment: undefined
};
static defineOption = function (name: string, val: any, setter: Function) { };
static isWordChar = function (ch: string) {
Expand Down Expand Up @@ -230,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 @@ -265,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 @@ -546,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 @@ -587,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 @@ -663,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 @@ -716,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 @@ -785,7 +791,9 @@ export class CodeMirror {
return hardWrap(this, options);
}

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

type Mutable<Type> = {
Expand Down Expand Up @@ -952,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
121 changes: 76 additions & 45 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type vimState = {
}
export type Marker = ReturnType<CodeMirror["setBookmark"]>
export type LineHandle = ReturnType<CodeMirror["getLineHandle"]>
export type Pos = { line: number, ch: number }
export type Pos = { line: number, ch: number, sticky?: string }

export interface CM5Range {
anchor: Pos,
Expand Down Expand Up @@ -72,14 +72,15 @@ export type OperatorArgs = {
}
// version of CodeMirror with vim state checked
export type CodeMirrorV = CodeMirror & {state: {vim: vimState}}
export type OperatorFn = (cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[], oldAnchor?: Pos, newHead?: Pos) => Pos|void
export type OperatorFn = (cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[], oldAnchor: Pos, newHead?: Pos) => Pos|void
export type vimOperators = {
change(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[]): void,
delete(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[]): void,
indent(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[]): void,
indentAuto(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[]): void,
changeCase(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[], oldAnchor: Pos, newHead: Pos): Pos,
yank(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[], oldAnchor: Pos): Pos,
hardWrap(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[], oldAnchor: Pos): Pos|void,
changeCase(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[], oldAnchor: Pos, newHead?: Pos): Pos|void,
yank(cm: CodeMirrorV, args: OperatorArgs, ranges: CM5RangeInterface[], oldAnchor: Pos): Pos|void,
} & {
[key: string]: OperatorFn
}
Expand Down Expand Up @@ -114,7 +115,7 @@ export type vimActions = {
[key: string]: ActionFn
}

export type MotionArgs = {
export type MotionArgsPartial = {
repeat?: number,
forward?: boolean,
selectedCharacter?: string,
Expand All @@ -130,7 +131,9 @@ export type MotionArgs = {
bigWord?: boolean,
repeatIsExplicit?: boolean,
noRepeat?: boolean
}
};

export type MotionArgs = MotionArgsPartial & {repeat: number};

export type MotionFn = (cm: CodeMirrorV, head: Pos, motionArgs: MotionArgs, vim: vimState, inputState: InputStateInterface) => Pos|[Pos,Pos]|null|undefined
export type vimMotions = {
Expand Down Expand Up @@ -180,29 +183,49 @@ export type vimOption = {

export type ExFn = ()=> void;


declare class StringStream {
string: string;
pos: number;
start: number;
constructor(string: string);
eol(): boolean;
sol(): boolean;
peek(): string | undefined;
next(): string | void;
eat(match: string | RegExp | ((ch: string) => boolean)): string | void;
eatWhile(match: string | RegExp | ((ch: string) => boolean)): boolean;
eatSpace(): boolean;
skipToEnd(): void;
skipTo(ch: string): boolean | void;
backUp(n: number): void;
column(): number;
indentation(): number;
match(pattern: string | RegExp, consume?: boolean, caseInsensitive?: boolean): boolean | RegExpMatchArray | null;
current(): string;
type allCommands = {
keys: string,
context?: string,
interlaceInsertRepeat?: boolean,
exitVisualBlock?: boolean,
isEdit?: boolean,
repeatOverride?: number
}

export {StringStream}
export type motionCommand = allCommands & {
type: 'motion',
motion: string,
motionArgs?: MotionArgsPartial,
repeatOverride?: number
}
export type operatorCommand = allCommands & {
type: 'operator',
operator: string,
operatorArgs?: OperatorArgs
}
export type actionCommand = allCommands & {
type: 'action',
action: string,
actionArgs?: ActionArgs,
motion?: string,
operator?: string,
interlaceInsertRepeat?: boolean
}
export type searchCommand = allCommands & {
type: 'search',
searchArgs: SearchArgs
}
export type operatorMotionCommand = allCommands & {
type: 'operatorMotion',
motion: string,
operator: string,
motionArgs?: MotionArgsPartial,
operatorArgs?: OperatorArgs,
operatorMotionArgs?: { [arg: string]: boolean | string }
}
export type idleCommand = allCommands & { type: 'idle' }
export type exCommand = allCommands & { type: 'ex' }
export type keyToExCommand = allCommands & { type: 'keyToEx', exArgs: { [arg: string]: any } }
export type keyToKeyCommand = allCommands & { toKeys: string, type: 'keyToKey' }

export type vimKey =
motionCommand
Expand All @@ -214,23 +237,6 @@ export type vimKey =
| exCommand
| keyToExCommand
| keyToKeyCommand;
type allCommands = { keys: string, context?: string, interlaceInsertRepeat?: boolean, exitVisualBlock?: boolean, isEdit?: boolean, }
export type motionCommand = allCommands & { type: 'motion', motion: string, motionArgs?: MotionArgs, repeatOverride?: number }
export type operatorCommand = allCommands & { type: 'operator', operator: string, operatorArgs?: OperatorArgs }
export type actionCommand = allCommands & { type: 'action', action: string, actionArgs?: ActionArgs, motion?: string, operator?: string}
export type searchCommand = allCommands & { type: 'search', searchArgs: SearchArgs }
export type operatorMotionCommand = allCommands & {
type: 'operatorMotion',
motion: string,
operator: string,
motionArgs?: MotionArgs,
operatorArgs?: OperatorArgs,
operatorMotionArgs?: { [arg: string]: boolean | string }
}
export type idleCommand = allCommands & { type: 'idle' }
export type exCommand = allCommands & { type: 'ex' }
export type keyToExCommand = allCommands & { type: 'keyToEx', exArgs: {[arg: string]: any} }
export type keyToKeyCommand = allCommands & { toKeys: string, type: 'keyToKey' }

export type vimKeyMap = vimKey[];

Expand Down Expand Up @@ -298,5 +304,30 @@ export type InsertModeChanges = {
expectCursorActivityForChange: any;
visualBlock?: number,
maybeReset?: boolean,
ignoreCount?: number,
repeatOverride?: number,
}

export type ExParams = {
commandName: string,
argString: string,
input: string,
args?: string[],

line: number,
lineEnd?: number,
selectionLine: number,
selectionLineEnd?: number,

setCfg?: Object,
callback?: any,

}


declare global {
function isNaN(v: any): v is Exclude<typeof v, number>;
interface String {
trimStart(): string
}
}
Loading

0 comments on commit f390c93

Please sign in to comment.