1+ import type { LRParser } from "@lezer/lr" ;
2+ import { parser } from "./syntax.grammar" ;
3+ import {
4+ LRLanguage ,
5+ LanguageSupport ,
6+ indentNodeProp ,
7+ foldNodeProp ,
8+ foldInside ,
9+ continuedIndent
10+ } from "@codemirror/language" ;
11+ import { parseMixed } from "@lezer/common" ;
12+ import { styleTags , tags } from "@lezer/highlight" ;
13+ import { xml } from "./xml" ;
14+
15+ function docCommentXmlOverlay ( from : number , content : string ) {
16+ const ranges : { from : number ; to : number } [ ] = [ ] ;
17+ let i = 0 ;
18+ while ( i < content . length ) {
19+ // Each segment starts with ///
20+ if ( content [ i ] === '/' && content [ i + 1 ] === '/' && content [ i + 2 ] === '/' ) {
21+ i += 3 ;
22+ if ( content [ i ] === ' ' ) { i ++ ; }
23+ const lineStart = i ;
24+ while ( i < content . length &&
25+ content [ i ] !== '\r' && content [ i ] !== '\n' &&
26+ content [ i ] !== '\u0085' && content [ i ] !== '\u2028' && content [ i ] !== '\u2029' ) {
27+ i ++ ;
28+ }
29+ if ( i > lineStart ) { ranges . push ( { from : from + lineStart , to : from + i } ) ; }
30+ }
31+ // Advance past newline and leading whitespace
32+ while ( i < content . length &&
33+ ( content [ i ] === '\r' || content [ i ] === '\n' ||
34+ content [ i ] === '\u0085' || content [ i ] === '\u2028' || content [ i ] === '\u2029' ||
35+ content [ i ] === ' ' || content [ i ] === '\t' ) ) {
36+ i ++ ;
37+ }
38+ }
39+ return ranges . length > 0 ? ranges : null ;
40+ }
41+
42+ export const csharpLanguage = LRLanguage . define ( {
43+ parser : parser . configure ( {
44+ props : [
45+ indentNodeProp . add ( {
46+ Delim : continuedIndent ( { except : / ^ \s * (?: c a s e \b | d e f a u l t : ) / } )
47+ } ) ,
48+ foldNodeProp . add ( {
49+ Delim : foldInside
50+ } ) ,
51+ styleTags ( {
52+ "Keyword ContextualKeyword SimpleType" : tags . keyword ,
53+ BooleanLiteral : tags . bool ,
54+ NullLiteral : tags . null ,
55+ IntegerLiteral : tags . integer ,
56+ RealLiteral : tags . float ,
57+ 'StringLiteral UTF8StringLiteral RawStringLiteral CharacterLiteral InterpolatedRegularString InterpolatedVerbatimString InterpolatedRawString $" @$" $@"' :
58+ tags . string ,
59+ LineComment : tags . lineComment ,
60+ BlockComment : tags . blockComment ,
61+ DocComment : tags . docComment ,
62+
63+ ". .. : Astrisk Slash % + - ++ -- Not ~ << & | ^ && || < > <= >= == NotEq = += -= *= SlashEq %= &= |= ^= ? ?? ??= =>" :
64+ tags . operator ,
65+
66+ PP_Directive : tags . keyword ,
67+
68+ TypeIdentifier : tags . typeName ,
69+ "ArgumentName AttrsNamedArg" : tags . variableName ,
70+ ConstName : tags . constant ( tags . variableName ) ,
71+
72+ //Ident: tags.name,
73+ MethodName : tags . function ( tags . variableName ) ,
74+ ParamName : [ tags . emphasis , tags . variableName ] ,
75+ VarName : tags . variableName ,
76+ "FieldName PropertyName" : tags . propertyName ,
77+
78+ "( )" : tags . paren ,
79+ "{ }" : tags . brace ,
80+ "[ ]" : tags . squareBracket
81+ } )
82+ ] ,
83+ wrap : parseMixed ( ( node , input ) => {
84+ if ( node . name !== "DocComment" ) {
85+ return null ;
86+ }
87+ const content = input . read ( node . from , node . to ) ;
88+ if ( content . indexOf ( "<" ) === - 1 ) {
89+ return null ;
90+ }
91+ const overlay = docCommentXmlOverlay ( node . from , content ) ;
92+ if ( ! overlay ) {
93+ return null ;
94+ }
95+ return {
96+ parser : ( xml . language . parser as LRParser ) . configure ( {
97+ props : [ styleTags ( { Text : tags . docComment } ) ]
98+ } ) ,
99+ overlay
100+ } ;
101+ } )
102+ } ) ,
103+ languageData : {
104+ commentTokens : { line : "//" , block : { open : "/*" , close : "*/" } } ,
105+ closeBrackets : { brackets : [ "(" , "[" , "{" , '"' , "'" , '"""' ] } ,
106+ indentOnInput : / ^ \s * ( ( \) | \] | \} ) $ | ( e l s e | e l s e \s + i f | c a t c h | f i n a l l y | c a s e ) \b | d e f a u l t : ) /
107+ }
108+ } ) ;
109+
110+ import { Prec } from "@codemirror/state" ;
111+ import { continueDocComment } from "./keymap" ;
112+ import { csharpCompletion } from "./complete" ;
113+ export function csharp ( ) {
114+ return new LanguageSupport ( csharpLanguage , [
115+ Prec . high ( continueDocComment ) ,
116+ csharpLanguage . data . of ( {
117+ autocomplete : csharpCompletion ( )
118+ } ) ,
119+ xml . support
120+ ] ) ;
121+ }
0 commit comments