1
1
<template >
2
2
<div >
3
3
<Codemirror
4
+ id =" cm-component"
4
5
v-model:value =" code"
5
6
:options =" cmOptions"
6
7
border
7
- :height = " 200 "
8
+ @ready = " onEditorReady "
8
9
:width =" 400"
10
+ :height =" 300"
9
11
/>
10
12
</div >
11
13
</template >
12
- <script lang="ts">
14
+ <script lang="ts" setup >
13
15
import { ref } from " vue" ;
14
16
import type { EditorConfiguration } from " codemirror" ;
15
17
import Codemirror from " codemirror-editor-vue3" ;
@@ -20,34 +22,97 @@ import Codemirror from "codemirror-editor-vue3";
20
22
21
23
// language
22
24
import " codemirror/mode/javascript/javascript.js" ;
25
+ import " codemirror/lib/codemirror.css" ;
26
+ import " codemirror/addon/hint/show-hint.css" ;
27
+ import " codemirror/mode/javascript/javascript" ;
28
+ import " codemirror/addon/hint/show-hint" ;
29
+ import " codemirror/addon/hint/javascript-hint" ;
30
+ const code = ref (` function getCompletions(token, context) {
31
+ var found = [], start = token.string;
32
+ function maybeAdd(str) {
33
+ if (str.indexOf(start) == 0) found.push(str);
34
+ }
35
+ function gatherCompletions(obj) {
36
+ if (typeof obj == "string") forEach(stringProps, maybeAdd);
37
+ else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
38
+ else if (obj instanceof Function) forEach(funcProps, maybeAdd);
39
+ for (var name in obj) maybeAdd(name);
40
+ }
23
41
24
- export default defineComponent ({
25
- components: {
26
- Codemirror ,
27
- },
28
- setup() {
29
- const code = ref (` function findSequence(goal) {
30
- function find(start, history) {
31
- if (start == goal)
32
- return history;
33
- else if (start > goal)
34
- return null;
35
- else
36
- return find(start + 5, "(" + history + " + 5)") ||
37
- find(start * 3, "(" + history + " * 3)");
38
- }
39
- return find(1, "1");
40
- } ` );
41
-
42
- const cmOptions: EditorConfiguration = {
43
- mode: " application/typescript" ,
44
- lineWiseCopyCut: true ,
45
- };
42
+ if (context) {
43
+ var obj = context.pop(), base;
44
+ if (obj.className == "js-variable")
45
+ base = window[obj.string];
46
+ else if (obj.className == "js-string")
47
+ base = "";
48
+ else if (obj.className == "js-atom")
49
+ base = 1;
50
+ while (base != null && context.length)
51
+ base = base[context.pop().string];
52
+ if (base != null) gatherCompletions(base);
53
+ }
54
+ else {
55
+ for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
56
+ gatherCompletions(window);
57
+ forEach(keywords, maybeAdd);
58
+ }
59
+ return found;
60
+ } ` );
46
61
47
- return {
48
- code ,
49
- cmOptions ,
50
- };
62
+ const cmOptions: EditorConfiguration = {
63
+ lineNumbers: true ,
64
+ mode: { name: " javascript" },
65
+ extraKeys: {
66
+ // "Ctrl-Space": "autocomplete",
67
+ " Ctrl-Space" : () => {
68
+ alert (" Ctrl-Space" );
69
+ },
51
70
},
52
- });
71
+ };
72
+
73
+ function synonyms(cm : any , option : any ) {
74
+ const comp = [
75
+ [" here" , " hither" ],
76
+ [" asynchronous" , " nonsynchronous" ],
77
+ [" completion" , " achievement" , " conclusion" , " culmination" , " expirations" ],
78
+ [" hinting" , " advise" , " broach" , " imply" ],
79
+ [" function" , " action" ],
80
+ [" provide" , " add" , " bring" , " give" ],
81
+ [" synonyms" , " equivalents" ],
82
+ [" words" , " token" ],
83
+ [" each" , " every" ],
84
+ ];
85
+ return new Promise ((accept ) => {
86
+ setTimeout (() => {
87
+ const cursor = cm .getCursor (),
88
+ line = cm .getLine (cursor .line );
89
+ let start = cursor .ch ,
90
+ end = cursor .ch ;
91
+ while (start && / \w / .test (line .charAt (start - 1 ))) -- start ;
92
+ while (end < line .length && / \w / .test (line .charAt (end ))) ++ end ;
93
+ const word = line .slice (start , end ).toLowerCase ();
94
+ for (let i = 0 ; i < comp .length ; i ++ ) {
95
+ if (comp [i ].indexOf (word ) !== - 1 ) {
96
+ return accept ({
97
+ list: comp [i ],
98
+ from: cm .pos (cursor .line , start ),
99
+ to: cm .pos (cursor .line , end ),
100
+ });
101
+ }
102
+ }
103
+ return accept (null );
104
+ }, 100 );
105
+ });
106
+ }
107
+ const onEditorReady = (cm : any ) => {
108
+ if (typeof Promise !== " undefined" ) {
109
+ cm .setOption (" hintOptions" , { hint: synonyms });
110
+ }
111
+ };
53
112
</script >
113
+
114
+ <style >
115
+ /* #cm-component .CodeMirror {
116
+ font-family: "Fira Code", monospace;
117
+ } */
118
+ </style >
0 commit comments