1
1
import { LanguageSupport , StreamLanguage } from "@codemirror/language" ;
2
- import { tags as t } from ' @lezer/highlight' ;
2
+ import { tags as t } from " @lezer/highlight" ;
3
3
4
- const zoektLanguage = StreamLanguage . define ( {
5
- token : ( stream ) => {
6
- if ( stream . match ( / - ? ( f i l e | b r a n c h | r e v i s i o n | r e v | c a s e | r e p o | l a n g | c o n t e n t | s y m | a r c h i v e d | f o r k | p u b l i c ) : / ) ) {
7
- return t . keyword . toString ( ) ;
8
- }
4
+ export const zoekt = ( ) => {
5
+ const zoektLanguage = StreamLanguage . define ( {
6
+ startState ( ) {
7
+ return {
8
+ inString : false ,
9
+ escaped : false
10
+ } ;
11
+ } ,
12
+ token ( stream , state ) {
13
+ // Handle strings
14
+ if ( state . inString ) {
15
+ if ( state . escaped ) {
16
+ state . escaped = false ;
17
+ stream . next ( ) ;
18
+ return t . string . toString ( ) ;
19
+ }
20
+ const ch = stream . next ( ) ;
21
+ if ( ch === "\\" ) {
22
+ state . escaped = true ;
23
+ return t . string . toString ( ) ;
24
+ } else if ( ch === '"' ) {
25
+ // End of string
26
+ state . inString = false ;
27
+ return t . string . toString ( ) ;
28
+ } else {
29
+ return t . string . toString ( ) ;
30
+ }
31
+ }
9
32
10
- if ( stream . match ( / \b o r \b / ) ) {
11
- return t . keyword . toString ( ) ;
12
- }
33
+ // Skip whitespace
34
+ if ( stream . eatSpace ( ) ) {
35
+ return null ;
36
+ }
13
37
14
- if ( stream . match ( / ( \( | \) ) / ) ) {
15
- return t . paren . toString ( ) ;
16
- }
38
+ // Negation operator
39
+ if ( stream . match ( / - / ) ) {
40
+ return t . operator . toString ( ) ;
41
+ }
17
42
18
- stream . next ( ) ;
19
- return null ;
20
- } ,
21
- } ) ;
43
+ // Parentheses
44
+ if ( stream . match ( "(" ) || stream . match ( ")" ) ) {
45
+ return t . paren . toString ( ) ;
46
+ }
47
+
48
+ // Check for prefixes first
49
+ // If these match, we return 'keyword'
50
+ if ( stream . match ( / ( a r c h i v e d : | b r a n c h : | b : | c : | c a s e : | c o n t e n t : | f : | f i l e : | f o r k : | p u b l i c : | r : | r e p o : | r e g e x : | l a n g : | s y m : | t : | t y p e : ) / ) ) {
51
+ return t . keyword . toString ( ) ;
52
+ }
53
+
54
+ // Now try matching a standalone word
55
+ // If the word is "or", return keyword; else nothing special
56
+ if ( stream . match ( / [ A - Z a - z 0 - 9 _ ] + / ) ) {
57
+ const word = stream . current ( ) ;
58
+ if ( word === "or" ) {
59
+ return t . keyword . toString ( ) ;
60
+ }
61
+ return null ;
62
+ }
63
+
64
+ // Double-quoted string start
65
+ if ( stream . peek ( ) === '"' ) {
66
+ stream . next ( ) ; // consume opening quote
67
+ state . inString = true ;
68
+ return t . string . toString ( ) ;
69
+ }
70
+
71
+ // If we reach here, consume a single character and return null
72
+ stream . next ( ) ;
73
+ return null ;
74
+ }
75
+ } ) ;
22
76
23
- export const zoekt = ( ) => {
24
77
return new LanguageSupport ( zoektLanguage ) ;
25
- }
78
+ } ;
0 commit comments