Skip to content

Commit

Permalink
feat: ✨ Add C# support (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek authored Jan 17, 2025
1 parent b3a63f6 commit 765ce4c
Show file tree
Hide file tree
Showing 18 changed files with 500 additions and 88 deletions.
6 changes: 3 additions & 3 deletions codelimit/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def check_command(paths: list[Path], quiet: bool):
check_file(abs_path, check_result)
exit_code = 1 if check_result.unmaintainable > 0 else 0
if (
not quiet
or check_result.hard_to_maintain > 0
or check_result.unmaintainable > 0
not quiet
or check_result.hard_to_maintain > 0
or check_result.unmaintainable > 0
):
check_result.report()
raise typer.Exit(code=exit_code)
Expand Down
4 changes: 3 additions & 1 deletion codelimit/common/Language.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from abc import ABC, abstractmethod

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

@abstractmethod
def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
pass
3 changes: 2 additions & 1 deletion codelimit/common/report/format_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@ def _print_findings_with_repository(report_units: list[ReportUnit],
link = (f'https://github.com/{owner}/{name}/blob/{branch}/{unit.file}#L{unit.measurement.start.line}-L'
f'{unit.measurement.end.line}')
console.print(
f"| {violation_type} \[{unit.measurement.unit_name}]({link}) | {unit.measurement.value} | {unit.file} |"
f"| {violation_type} " + '\[' + unit.measurement.unit_name + ']' + f"({link}) | {unit.measurement.value} "
f"| {unit.file} |"
)
2 changes: 1 addition & 1 deletion codelimit/common/token_matching/predicate/TokenValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def __hash__(self):
return hash(self.value)

def __str__(self):
return f"<TokenValue {self.value}>"
return self.value
20 changes: 20 additions & 0 deletions codelimit/languages/CSharp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from codelimit.common.Language import Language
from codelimit.common.gsm.operator.OneOrMore import OneOrMore
from codelimit.common.scope.scope_utils import (
get_blocks,
get_headers,
)
from codelimit.common.token_matching.predicate.Balanced import Balanced
from codelimit.common.token_matching.predicate.Name import Name
from codelimit.common.token_matching.predicate.Symbol import Symbol


class CSharp(Language):
def __init__(self):
super().__init__('C#')

def extract_headers(self, tokens: list) -> list:
return get_headers(tokens, [Name(), OneOrMore(Balanced('(', ')'))], Symbol('{'))

def extract_blocks(self, tokens: list, headers: list) -> list:
return get_blocks(tokens, "{", "}")
2 changes: 1 addition & 1 deletion codelimit/languages/Cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def extract_headers(self, tokens: list[Token]) -> list[Header]:
return get_headers(tokens, [Name(), OneOrMore(Balanced("(", ")"))], Symbol("{"))

def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
return get_blocks(tokens, "{", "}")
4 changes: 2 additions & 2 deletions codelimit/languages/TypeScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from codelimit.common.scope.Header import Header
from codelimit.common.scope.scope_utils import get_blocks, get_headers
from codelimit.common.token_matching.predicate.Balanced import Balanced
from codelimit.common.token_matching.predicate.Or import Or
from codelimit.common.token_matching.predicate.Keyword import Keyword
from codelimit.common.token_matching.predicate.Name import Name
from codelimit.common.token_matching.predicate.Operator import Operator
from codelimit.common.token_matching.predicate.Or import Or
from codelimit.common.token_matching.predicate.Symbol import Symbol


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

def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
return get_blocks(tokens, "{", "}")
16 changes: 8 additions & 8 deletions codelimit/languages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from codelimit.common.Language import Language
from codelimit.languages.C import C
from codelimit.languages.CSharp import CSharp
from codelimit.languages.Cpp import Cpp
from codelimit.languages.Java import Java
from codelimit.languages.JavaScript import JavaScript
Expand All @@ -9,16 +11,14 @@
class Languages:
C = C()
Cpp = Cpp()
CSharp = CSharp()
Java = Java()
JavaScript = JavaScript()
Python = Python()
TypeScript = TypeScript()

by_name = {
C.name: C,
Cpp.name: Cpp,
Java.name: Java,
JavaScript.name: JavaScript,
Python.name: Python,
TypeScript.name: TypeScript,
}
by_name: dict[str, Language] = {}
for subclass in Language.__subclasses__():
language = subclass() # type: ignore
by_name[language.name] = language

Loading

0 comments on commit 765ce4c

Please sign in to comment.