diff --git a/extension.toml b/extension.toml index cbed3a0..718cc91 100644 --- a/extension.toml +++ b/extension.toml @@ -17,6 +17,8 @@ language = "CSharp" name = "Roslyn" language = "CSharp" +snippets = ["./languages/csharp/snippets/csharp.json"] + [grammars.c_sharp] repository = "https://github.com/tree-sitter/tree-sitter-c-sharp" commit = "485f0bae0274ac9114797fc10db6f7034e4086e3" diff --git a/languages/csharp/config.toml b/languages/csharp/config.toml index 8f07b45..b527aea 100644 --- a/languages/csharp/config.toml +++ b/languages/csharp/config.toml @@ -3,6 +3,7 @@ code_fence_block_name = "csharp" grammar = "c_sharp" path_suffixes = ["cs"] line_comments = ["// ", "/// "] +block_comment = ["/*", "*/"] autoclose_before = ";:.,=}])>" brackets = [ { start = "{", end = "}", close = true, newline = true }, diff --git a/languages/csharp/folds.scm b/languages/csharp/folds.scm new file mode 100644 index 0000000..6f6e8a8 --- /dev/null +++ b/languages/csharp/folds.scm @@ -0,0 +1,45 @@ +(class_declaration + body: (_ "{" "}")) @fold + +(struct_declaration + body: (_ "{" "}")) @fold + +(interface_declaration + body: (_ "{" "}")) @fold + +(enum_declaration + body: (_ "{" "}")) @fold + +(record_declaration + body: (_ "{" "}")) @fold + +(namespace_declaration + body: (_ "{" "}")) @fold + +(method_declaration + body: (_ "{" "}")) @fold + +(property_declaration + body: (_ "{" "}")) @fold + +(accessor_list + "{" "}") @fold + +(initializer_expression + "{" "}") @fold + +(lambda_expression + body: (_ "{" "}")) @fold + +(switch_statement + "{" "}") @fold + +(preprocessor_if + ("#if") @fold + ("#endif") @fold) @fold + +(preprocessor_region + ("#region") @fold + ("#endregion") @fold) @fold + +(comment) @fold diff --git a/languages/csharp/locals.scm b/languages/csharp/locals.scm new file mode 100644 index 0000000..14d07c0 --- /dev/null +++ b/languages/csharp/locals.scm @@ -0,0 +1,62 @@ +(method_declaration + body: (block) @local-scope) + +(local_function_statement + body: (block) @local-scope) + +(property_declaration + body: (_) @local-scope) + +(accessor_declaration + body: (block) @local-scope) + +(for_statement + body: (_) @local-scope) + +(for_each_statement + body: (_) @local-scope) + +(while_statement + body: (_) @local-scope) + +(do_statement + body: (_) @local-scope) + +(if_statement + body: (_) @local-scope) + +(switch_statement + body: (_) @local-scope) + +(using_statement + body: (_) @local-scope) + +(lock_statement + body: (_) @local-scope) + +(lambda_expression + body: (_) @local-scope) + +(anonymous_method_expression + body: (_) @local-scope) + +(variable_declaration + (variable_declarator + name: (identifier) @definition)) + +(parameter + name: (identifier) @definition) + +(parameter + name: (identifier) @definition.parameter) + +(for_each_statement + (identifier) @definition) + +(from_clause + (identifier) @definition) + +(let_clause + (identifier) @definition) + +(identifier) @reference diff --git a/languages/csharp/overrides.scm b/languages/csharp/overrides.scm new file mode 100644 index 0000000..5ee2107 --- /dev/null +++ b/languages/csharp/overrides.scm @@ -0,0 +1,9 @@ +(string_literal) @string + +(verbatim_string_literal) @string + +(raw_string_literal) @string + +(interpolated_string_expression) @string + +(comment) @comment.inclusive diff --git a/languages/csharp/snippets/csharp.json b/languages/csharp/snippets/csharp.json new file mode 100644 index 0000000..745cbed --- /dev/null +++ b/languages/csharp/snippets/csharp.json @@ -0,0 +1,280 @@ +{ + "Class": { + "prefix": "class", + "body": [ + "class ${1:MyClass}", + "{", + "\t$0", + "}" + ], + "description": "Class declaration" + }, + "Constructor": { + "prefix": "ctor", + "body": [ + "public ${1:ClassName}($2)", + "{", + "\t$0", + "}" + ], + "description": "Constructor" + }, + "Property (full)": { + "prefix": "propfull", + "body": [ + "private ${1:int} _${2:field};", + "", + "public ${1:int} ${2:field}", + "{", + "\tget => _${2:field};", + "\tset => _${2:field} = value;", + "}", + "$0" + ], + "description": "Full property with backing field" + }, + "Property (auto)": { + "prefix": "prop", + "body": [ + "public ${1:int} ${2:MyProperty} { get; set; }", + "$0" + ], + "description": "Auto-implemented property" + }, + "Property (getter only)": { + "prefix": "propget", + "body": [ + "public ${1:int} ${2:MyProperty} { get; }", + "$0" + ], + "description": "Getter-only property" + }, + "Interface": { + "prefix": "interface", + "body": [ + "interface I${1:MyInterface}", + "{", + "\t$0", + "}" + ], + "description": "Interface declaration" + }, + "Struct": { + "prefix": "struct", + "body": [ + "struct ${1:MyStruct}", + "{", + "\t$0", + "}" + ], + "description": "Struct declaration" + }, + "Enum": { + "prefix": "enum", + "body": [ + "enum ${1:MyEnum}", + "{", + "\t${2:Value1},", + "\t${3:Value2}", + "}", + "$0" + ], + "description": "Enum declaration" + }, + "Main method": { + "prefix": "main", + "body": [ + "static void Main(string[] args)", + "{", + "\t$0", + "}" + ], + "description": "Main method" + }, + "for loop": { + "prefix": "for", + "body": [ + "for (int ${1:i} = 0; ${1:i} < ${2:length}; ${1:i}++)", + "{", + "\t$0", + "}" + ], + "description": "For loop" + }, + "foreach loop": { + "prefix": "foreach", + "body": [ + "foreach (var ${1:item} in ${2:collection})", + "{", + "\t$0", + "}" + ], + "description": "Foreach loop" + }, + "while loop": { + "prefix": "while", + "body": [ + "while (${1:condition})", + "{", + "\t$0", + "}" + ], + "description": "While loop" + }, + "do-while loop": { + "prefix": "dowhile", + "body": [ + "do", + "{", + "\t$0", + "} while (${1:condition});" + ], + "description": "Do-while loop" + }, + "if statement": { + "prefix": "if", + "body": [ + "if (${1:condition})", + "{", + "\t$0", + "}" + ], + "description": "If statement" + }, + "if-else statement": { + "prefix": "ifelse", + "body": [ + "if (${1:condition})", + "{", + "\t$2", + "}", + "else", + "{", + "\t$3", + "}" + ], + "description": "If-else statement" + }, + "switch statement": { + "prefix": "switch", + "body": [ + "switch (${1:expression})", + "{", + "\tcase ${2:value}:", + "\t\t$0", + "\t\tbreak;", + "\tdefault:", + "\t\tbreak;", + "}" + ], + "description": "Switch statement" + }, + "try-catch": { + "prefix": "try", + "body": [ + "try", + "{", + "\t$0", + "}", + "catch (System.Exception ex)", + "{", + "\tthrow;", + "}" + ], + "description": "Try-catch block" + }, + "try-finally": { + "prefix": "tryfinally", + "body": [ + "try", + "{", + "\t$0", + "}", + "finally", + "{", + "\t", + "}" + ], + "description": "Try-finally block" + }, + "using directive": { + "prefix": "using", + "body": "using ${1:System};$0", + "description": "Using directive" + }, + "namespace declaration": { + "prefix": "ns", + "body": [ + "namespace ${1:MyNamespace}", + "{", + "\t$0", + "}" + ], + "description": "Namespace declaration" + }, + "LINQ query": { + "prefix": "from", + "body": [ + "var ${1:result} = from ${2:x} in ${3:source}", + "\t\t\twhere ${4:condition}", + "\t\t\tselect ${2:x};", + "$0" + ], + "description": "LINQ query expression" + }, + "Console.WriteLine": { + "prefix": "cw", + "body": "Console.WriteLine(${1:message});$0", + "description": "Console.WriteLine" + }, + "StringBuilder": { + "prefix": "sb", + "body": [ + "var ${1:sb} = new System.Text.StringBuilder();", + "$0" + ], + "description": "StringBuilder declaration" + }, + "equals override": { + "prefix": "equals", + "body": [ + "public override bool Equals(object? obj)", + "{", + "\treturn obj is ${1:MyType} other &&", + "\t\t${2:EqualityComparer<${3:string}>.Default.Equals(${4:field}, other.${4:field})};", + "}", + "", + "public override int GetHashCode()", + "{", + "\treturn HashCode.Combine(${4:field});", + "}", + "$0" + ], + "description": "Equals and GetHashCode overrides" + }, + "ToString override": { + "prefix": "tostring", + "body": [ + "public override string ToString()", + "{", + "\treturn $\"${1:{$$2:field}}\";", + "}", + "$0" + ], + "description": "ToString override" + }, + "null check": { + "prefix": "null", + "body": "ArgumentNullException.ThrowIfNull(${1:paramName});$0", + "description": "Null check guard" + }, + "region block": { + "prefix": "region", + "body": [ + "#region ${1:Region Name}", + "$0", + "#endregion" + ], + "description": "Region block" + } +} diff --git a/languages/csharp/textobjects.scm b/languages/csharp/textobjects.scm index 2285817..2c108c4 100644 --- a/languages/csharp/textobjects.scm +++ b/languages/csharp/textobjects.scm @@ -1,18 +1,31 @@ -;; Join up all the comments -(comment)+ @comment.around - -;; Standard methods -(method_declaration - body: (_ "{" (_)* @function.inside "}")) @function.around - -;; Standard classes -(class_declaration - body: (_ "{" (_)* @class.inside "}")) @class.around - -;; Interface declarations -(method_declaration) @function.around - -;; Lambda expressions -(lambda_expression - body: (_ "{"? (_)* @function.inside "}"? ) -) @function.around +(comment)+ @comment.around + +(method_declaration + body: (_ "{" (_)* @function.inside "}")) @function.around + +(class_declaration + body: (_ "{" (_)* @class.inside "}")) @class.around + +(struct_declaration + body: (_ "{" (_)* @class.inside "}")) @class.around + +(record_declaration + body: (_ "{" (_)* @class.inside "}")) @class.around + +(interface_declaration + body: (_ "{" (_)* @class.inside "}")) @class.around + +(enum_declaration + body: (_ "{" (_)* @class.inside "}")) @class.around + +(local_function_statement + body: (_ "{" (_)* @function.inside "}")) @function.around + +(lambda_expression + body: (_ "{"? (_)* @function.inside "}"?) +) @function.around + +(block) @block.around + +(property_declaration + body: (_ "{" (_)* @function.inside "}")) @function.around