Skip to content

Commit eee01e1

Browse files
committed
fix types
1 parent 3707320 commit eee01e1

File tree

7 files changed

+598
-321
lines changed

7 files changed

+598
-321
lines changed

dev/index.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { highlightActiveLine, keymap, Decoration, DecorationSet,
33
ViewPlugin, ViewUpdate, WidgetType, drawSelection } from '@codemirror/view';
44
import { javascript } from '@codemirror/lang-javascript';
55
import { xml } from '@codemirror/lang-xml';
6-
import { Vim, vim } from "../src/index"
6+
import { Vim, vim } from "../src/index" // "@replit/codemirror-vim"
77

88
import * as commands from "@codemirror/commands";
99
import { Annotation, Compartment, EditorState, Extension, Transaction, Range } from '@codemirror/state';
@@ -25,7 +25,8 @@ new EditorView({
2525
2626
`;
2727

28-
function addOption(name, description?, onclick?) {
28+
function addOption(name: "wrap"|"html"|"status"|"jj"|"split"|"readOnly",
29+
description?: string, onclick?: (value: boolean) => void) {
2930
let checkbox = document.createElement("input");
3031
checkbox.type = "checkbox";
3132
checkbox.id = name;
@@ -49,16 +50,16 @@ class TestWidget extends WidgetType {
4950
constructor(private side: number) {
5051
super();
5152
}
52-
eq(other) {
53-
return (true);
53+
override eq(other: TestWidget) {
54+
return other == this;
5455
}
5556
toDOM() {
5657
const wrapper = document.createElement('span');
5758
wrapper.textContent = " widget" + this.side + " "
5859
wrapper.style.opacity = '0.4';
5960
return wrapper;
6061
}
61-
ignoreEvent() {
62+
override ignoreEvent() {
6263
return false;
6364
}
6465
}
@@ -73,7 +74,7 @@ function widgets(view: EditorView) {
7374
})
7475
widgets.push(deco.range(200 + 10 * i))
7576
}
76-
console.log(widgets)
77+
console.log(widgets, view)
7778
return Decoration.set(widgets)
7879
}
7980

@@ -93,6 +94,7 @@ const testWidgetPlugin = ViewPlugin.fromClass(class {
9394

9495
eventHandlers: {
9596
mousedown: (e, view) => {
97+
console.log("mousedown", e, view)
9698
}
9799
}
98100
})
@@ -117,10 +119,12 @@ var options = {
117119

118120

119121
Vim.defineOption('wrap', false, 'boolean', null, function(val, cm) {
122+
if (!cm) console.log("should set global option");
120123
if (val == undefined) return options.wrap;
121-
var checkbox = document.getElementById("wrap");
124+
var checkbox = document.getElementById("wrap") as HTMLInputElement;
122125
if (checkbox) {
123126
checkbox.checked = val;
127+
//@ts-ignore
124128
checkbox.onclick();
125129
}
126130
});
@@ -166,21 +170,22 @@ var defaultExtensions = [
166170
]),
167171
]
168172

169-
function saveTab(name) {
173+
function saveTab(name: string) {
170174
return EditorView.updateListener.of((v) => {
171175
tabs[name] = v.state;
172176
})
173177
}
174178

175-
var tabs = {
179+
180+
var tabs: Record<string, EditorState> = {
176181
js: EditorState.create({
177182
doc: doc,
178183
extensions: [...defaultExtensions, javascript(), saveTab("js")]
179184
}),
180185
html: EditorState.create({
181186
doc: document.documentElement.outerHTML,
182187
extensions: [...defaultExtensions, testWidgetPlugin, xml(), saveTab("html")]
183-
})
188+
}),
184189
}
185190

186191
function updateView() {
@@ -204,17 +209,25 @@ function updateView() {
204209
})
205210
}
206211

212+
declare global {
213+
interface Window {
214+
blinkRate?: number;
215+
}
216+
}
217+
207218
function selectTab(tab: string) {
208-
if (view) view.setState(tabs[tab])
209-
if (view2) view2.setState(tabs[tab])
219+
let state = tabs[tab];
220+
if (!state) return;
221+
if (view) view.setState(state)
222+
if (view2) view2.setState(state)
210223
addLogListeners();
211224
}
212225

213226
Vim.defineEx("tabnext", "tabn", () => {
214-
tabs["scratch"] = EditorState.create({
227+
tabs.scratch = EditorState.create({
215228
doc: "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
216229
extensions: [defaultExtensions, saveTab("scratch")],
217-
})
230+
});
218231
selectTab("scratch")
219232
});
220233

@@ -304,7 +317,7 @@ updateView()
304317
// save and restor search history
305318

306319

307-
function saveHistory(name) {
320+
function saveHistory(name: "exCommandHistoryController"|"searchHistoryController") {
308321
var controller = Vim.getVimGlobalState_()[name];
309322
var json = JSON.stringify(controller);
310323
if (json.length > 10000) {
@@ -315,11 +328,11 @@ function saveHistory(name) {
315328
}
316329
localStorage[name] = json;
317330
}
318-
function restoreHistory(name) {
331+
function restoreHistory(name: "exCommandHistoryController"|"searchHistoryController") {
319332
try {
320333
var json = JSON.parse(localStorage[name]);
321334
var controller = Vim.getVimGlobalState_()[name];
322-
controller.historyBuffer = json.historyBuffer.filter(x => typeof x == "string" && x)
335+
controller.historyBuffer = json.historyBuffer.filter((x: unknown) => typeof x == "string" && x)
323336
controller.iterator = Math.min(parseInt(json.iterator) || Infinity, controller.historyBuffer.length)
324337
} catch(e) {
325338

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
"scripts": {
66
"dev": "vite ./dev",
77
"test": "cm-runtests",
8-
"static": "tsc --noEmit",
9-
"testAll": "yarn run static && yarn run test && cd dev/cm5 && yarn run buildAndTest",
10-
"build": "cm-buildhelper src/index.ts && node scripts/addVersion.cjs",
8+
"build-types": "rm -rf out && tsc && dts-bundle-generator out/index.ts -o dist/index.d.ts && cp dist/index.d.ts dist/index.d.cts",
9+
"testAll": "yarn run build-test-package && yarn run test && cd dev/cm5 && yarn run buildAndTest",
10+
"build": "cm-buildhelper src/index.ts && node scripts/addVersion.cjs && npm run build-types",
1111
"publish": "yarn run build && npm publish",
12+
"build-test-package": "bash ./scripts/build-test-package.sh",
1213
"prepare": "yarn run build"
1314
},
1415
"keywords": [
@@ -38,9 +39,14 @@
3839
"@codemirror/lang-xml": "^6.0.0",
3940
"@codemirror/language": "^6.1.0",
4041
"codemirror": "6.0.1",
41-
"typescript": "^5.0.2",
42-
"vite": "^2.9.6"
42+
"dts-bundle-generator": "^9.5.1",
43+
"typescript": "^5.7.3",
44+
"vite": "^6.1.0"
4345
},
46+
"files": [
47+
"dist",
48+
"README.md"
49+
],
4450
"repository": {
4551
"type": "git",
4652
"url": "https://github.com/replit/codemirror-vim.git"

scripts/build-test-package.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
set -euxo pipefail
3+
4+
# Navigate to the repository root
5+
cd "$(dirname "$0")/.."
6+
ROOT="$(pwd)"
7+
8+
# npm pack the repository
9+
rm -f replit-codemirror-vim-*.tgz
10+
npm pack
11+
12+
# Get the name of the packed file
13+
PACKAGE_FILE=$(ls replit-codemirror-vim-*.tgz | sort -V | tail -n 1)
14+
15+
mv "$PACKAGE_FILE" replit-codemirror-vim-latest.tgz
16+
17+
rm -rf ../.test_package
18+
mkdir -p ../.test_package
19+
cd ../.test_package
20+
21+
cp "$ROOT"/dev/index.ts index.ts
22+
ls
23+
sed -i '' 's|"../src/index" //||g' index.ts
24+
25+
echo '{
26+
"name": "test_package",
27+
"scripts": {
28+
"build": "tsc",
29+
"test": "echo \"No tests yet\""
30+
}
31+
}' > package.json
32+
33+
echo '{
34+
"compilerOptions": {
35+
"strict": true,
36+
"noImplicitAny": true,
37+
"strictNullChecks": true,
38+
"strictFunctionTypes": true,
39+
"strictBindCallApply": true,
40+
"strictPropertyInitialization": true,
41+
"noImplicitThis": true,
42+
"alwaysStrict": true,
43+
"noUnusedLocals": true,
44+
"noUnusedParameters": true,
45+
"noImplicitReturns": false,
46+
"noFallthroughCasesInSwitch": true,
47+
"noUncheckedIndexedAccess": true,
48+
"noImplicitOverride": true,
49+
"esModuleInterop": true,
50+
"forceConsistentCasingInFileNames": true,
51+
"module": "commonjs",
52+
"target": "es2020",
53+
"moduleResolution": "node"
54+
},
55+
"include": ["*.ts"],
56+
"exclude": ["node_modules"]
57+
}
58+
' > tsconfig.json
59+
60+
# Install the ace package from the npm pack result
61+
npm i "$ROOT"/replit-codemirror-vim-latest.tgz
62+
npm i codemirror @codemirror/lang-javascript @codemirror/lang-xml
63+
64+
# Install TypeScript
65+
npm i typescript@latest
66+
rm -f index.js
67+
npm run build
68+
69+
# Install old version of TypeScript
70+
npm i typescript@4
71+
rm -f index.js
72+
npm run build

src/cm_adapter.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ export class CodeMirror {
152152
return wordChar.test(ch);
153153
};
154154
static keys: any = keys;
155-
static addClass = function (el, str) { };
156-
static rmClass = function (el, str) { };
155+
static addClass = function (el: any, str: string) { };
156+
static rmClass = function (el: any, str: string) { };
157157
static e_preventDefault = function (e: Event) {
158158
e.preventDefault()
159159
};
@@ -172,7 +172,7 @@ export class CodeMirror {
172172
static signal = signal;
173173

174174
// --------------------------
175-
openDialog(template: Element, callback: Function, options: any) {
175+
openDialog(template: Element, callback: Function|undefined, options: any) {
176176
return openDialog(this, template, callback, options);
177177
};
178178
openNotification(template: Node, options: NotificationOptions) {
@@ -674,7 +674,7 @@ export class CodeMirror {
674674
curOp.cursorActivityHandlers = this._handlers["cursorActivity"] && this._handlers["cursorActivity"].slice();
675675
this.curOp.cursorActivity = true;
676676
};
677-
operation(fn: Function, force?: boolean) {
677+
operation<T>(fn: ()=>T, force?: boolean) {
678678
if (!this.curOp)
679679
this.curOp = { $d: 0 };
680680
this.curOp.$d++;
@@ -727,7 +727,7 @@ export class CodeMirror {
727727

728728
}
729729
};
730-
getOption(name:"firstLineNumber"|"tabSize"): number;
730+
getOption(name:"firstLineNumber"|"tabSize"|"textwidth"): number;
731731
getOption(name:string): number|boolean|string|undefined;
732732
getOption(name: string) {
733733
switch (name) {
@@ -875,7 +875,7 @@ function hideDialog(cm: CodeMirror, dialog: Element) {
875875
}
876876
}
877877

878-
function openDialog(me: CodeMirror, template: Element, callback: Function, options: any) {
878+
function openDialog(me: CodeMirror, template: Element, callback: Function|undefined, options: any) {
879879
if (!options) options = {};
880880

881881
closeNotification(me, undefined);
@@ -913,7 +913,7 @@ function openDialog(me: CodeMirror, template: Element, callback: Function, optio
913913

914914
CodeMirror.on(inp, "keydown", function (e: KeyboardEvent) {
915915
if (options && options.onKeyDown && options.onKeyDown(e, inp.value, close)) { return; }
916-
if (e.keyCode == 13) callback(inp.value);
916+
if (e.keyCode == 13) callback && callback(inp.value);
917917
if (e.keyCode == 27 || (options.closeOnEnter !== false && e.keyCode == 13)) {
918918
inp.blur();
919919
CodeMirror.e_stop(e);
@@ -968,7 +968,8 @@ function scanForBracket(cm: CodeMirror, where: Pos, dir: -1 | 1, style: any, con
968968
return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
969969
}
970970

971-
function findMatchingTag(cm: CodeMirror, pos: Pos): undefined {
971+
function findMatchingTag(cm: CodeMirror, pos: Pos) {
972+
return null;
972973
}
973974

974975
function findEnclosingTag(cm: CodeMirror, pos: Pos) {
@@ -1029,7 +1030,7 @@ class Marker {
10291030

10301031

10311032

1032-
function hardWrap(cm, options) {
1033+
function hardWrap(cm: CodeMirror, options: {from: number, to: number, column?: number, allowMerge?: boolean}) {
10331034
var max = options.column || cm.getOption('textwidth') || 80;
10341035
var allowMerge = options.allowMerge != false;
10351036

@@ -1066,7 +1067,7 @@ function hardWrap(cm, options) {
10661067
}
10671068
return row;
10681069

1069-
function findSpace(line, max, min) {
1070+
function findSpace(line: string, max: number, min: number) {
10701071
if (line.length < max)
10711072
return;
10721073
var before = line.slice(0, max);

0 commit comments

Comments
 (0)