Skip to content

Commit 765ce4c

Browse files
feat: ✨ Add C# support (#75)
1 parent b3a63f6 commit 765ce4c

18 files changed

Lines changed: 500 additions & 88 deletions

File tree

codelimit/commands/check.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def check_command(paths: list[Path], quiet: bool):
3030
check_file(abs_path, check_result)
3131
exit_code = 1 if check_result.unmaintainable > 0 else 0
3232
if (
33-
not quiet
34-
or check_result.hard_to_maintain > 0
35-
or check_result.unmaintainable > 0
33+
not quiet
34+
or check_result.hard_to_maintain > 0
35+
or check_result.unmaintainable > 0
3636
):
3737
check_result.report()
3838
raise typer.Exit(code=exit_code)

codelimit/common/Language.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from abc import ABC, abstractmethod
24

35
from codelimit.common.Token import Token
@@ -17,6 +19,6 @@ def extract_headers(self, tokens: list[Token]) -> list[Header]:
1719

1820
@abstractmethod
1921
def extract_blocks(
20-
self, tokens: list[Token], headers: list[Header]
22+
self, tokens: list[Token], headers: list[Header]
2123
) -> list[TokenRange]:
2224
pass

codelimit/common/report/format_markdown.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,6 @@ def _print_findings_with_repository(report_units: list[ReportUnit],
126126
link = (f'https://github.com/{owner}/{name}/blob/{branch}/{unit.file}#L{unit.measurement.start.line}-L'
127127
f'{unit.measurement.end.line}')
128128
console.print(
129-
f"| {violation_type} \[{unit.measurement.unit_name}]({link}) | {unit.measurement.value} | {unit.file} |"
129+
f"| {violation_type} " + '\[' + unit.measurement.unit_name + ']' + f"({link}) | {unit.measurement.value} "
130+
f"| {unit.file} |"
130131
)

codelimit/common/token_matching/predicate/TokenValue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ def __hash__(self):
2222
return hash(self.value)
2323

2424
def __str__(self):
25-
return f"<TokenValue {self.value}>"
25+
return self.value

codelimit/languages/CSharp.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from codelimit.common.Language import Language
2+
from codelimit.common.gsm.operator.OneOrMore import OneOrMore
3+
from codelimit.common.scope.scope_utils import (
4+
get_blocks,
5+
get_headers,
6+
)
7+
from codelimit.common.token_matching.predicate.Balanced import Balanced
8+
from codelimit.common.token_matching.predicate.Name import Name
9+
from codelimit.common.token_matching.predicate.Symbol import Symbol
10+
11+
12+
class CSharp(Language):
13+
def __init__(self):
14+
super().__init__('C#')
15+
16+
def extract_headers(self, tokens: list) -> list:
17+
return get_headers(tokens, [Name(), OneOrMore(Balanced('(', ')'))], Symbol('{'))
18+
19+
def extract_blocks(self, tokens: list, headers: list) -> list:
20+
return get_blocks(tokens, "{", "}")

codelimit/languages/Cpp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ def extract_headers(self, tokens: list[Token]) -> list[Header]:
1717
return get_headers(tokens, [Name(), OneOrMore(Balanced("(", ")"))], Symbol("{"))
1818

1919
def extract_blocks(
20-
self, tokens: list[Token], headers: list[Header]
20+
self, tokens: list[Token], headers: list[Header]
2121
) -> list[TokenRange]:
2222
return get_blocks(tokens, "{", "}")

codelimit/languages/TypeScript.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from codelimit.common.scope.Header import Header
77
from codelimit.common.scope.scope_utils import get_blocks, get_headers
88
from codelimit.common.token_matching.predicate.Balanced import Balanced
9-
from codelimit.common.token_matching.predicate.Or import Or
109
from codelimit.common.token_matching.predicate.Keyword import Keyword
1110
from codelimit.common.token_matching.predicate.Name import Name
1211
from codelimit.common.token_matching.predicate.Operator import Operator
12+
from codelimit.common.token_matching.predicate.Or import Or
1313
from codelimit.common.token_matching.predicate.Symbol import Symbol
1414

1515

@@ -38,6 +38,6 @@ def extract_headers(self, tokens: list[Token]) -> list[Header]:
3838
return functions + arrow_functions
3939

4040
def extract_blocks(
41-
self, tokens: list[Token], headers: list[Header]
41+
self, tokens: list[Token], headers: list[Header]
4242
) -> list[TokenRange]:
4343
return get_blocks(tokens, "{", "}")

codelimit/languages/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from codelimit.common.Language import Language
12
from codelimit.languages.C import C
3+
from codelimit.languages.CSharp import CSharp
24
from codelimit.languages.Cpp import Cpp
35
from codelimit.languages.Java import Java
46
from codelimit.languages.JavaScript import JavaScript
@@ -9,16 +11,14 @@
911
class Languages:
1012
C = C()
1113
Cpp = Cpp()
14+
CSharp = CSharp()
1215
Java = Java()
1316
JavaScript = JavaScript()
1417
Python = Python()
1518
TypeScript = TypeScript()
1619

17-
by_name = {
18-
C.name: C,
19-
Cpp.name: Cpp,
20-
Java.name: Java,
21-
JavaScript.name: JavaScript,
22-
Python.name: Python,
23-
TypeScript.name: TypeScript,
24-
}
20+
by_name: dict[str, Language] = {}
21+
for subclass in Language.__subclasses__():
22+
language = subclass() # type: ignore
23+
by_name[language.name] = language
24+

0 commit comments

Comments
 (0)