Skip to content

Commit 4c0de1b

Browse files
committed
fix: 🐛 Fix nocl exclusions
1 parent 93d6ef5 commit 4c0de1b

File tree

8 files changed

+51
-44
lines changed

8 files changed

+51
-44
lines changed

codelimit/common/Scanner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from pygments.lexer import Lexer
1010
from pygments.lexers import get_lexer_for_filename
1111
from pygments.util import ClassNotFound
12-
from rich.live import Live
1312
from rich import print
13+
from rich.live import Live
1414

1515
from codelimit.common.Codebase import Codebase
1616
from codelimit.common.Configuration import Configuration
@@ -153,7 +153,7 @@ def scan_file(tokens: list[Token], language: Language) -> list[Measurement]:
153153
last_token.location.column + len(last_token.value),
154154
)
155155
measurements.append(
156-
Measurement(scope.header.name, start_location, end_location, length)
156+
Measurement(scope.header.name(), start_location, end_location, length)
157157
)
158158
return measurements
159159

codelimit/common/scope/Header.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
@dataclass
88
class Header:
9-
name: str
9+
name_token: Token
1010
token_range: TokenRange
1111

12+
def name(self) -> str:
13+
return self.name_token.value
14+
1215

1316
def sort_headers(headers: list[Header], tokens: list[Token], reverse=False) -> list[Header]:
1417
return sorted(

codelimit/common/scope/scope_utils.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def build_scopes(tokens: list[Token], language: Language) -> list[Scope]:
1818
headers = language.extract_headers(code_tokens)
1919
blocks = language.extract_blocks(code_tokens, headers)
2020
scopes = _build_scopes_from_headers_and_blocks(headers, blocks, code_tokens)
21-
filtered_scopes = _filter_nocl_scopes(scopes, code_tokens, nocl_comment_tokens)
21+
filtered_scopes = _filter_nocl_scopes(scopes, nocl_comment_tokens)
2222
if language.allow_nested_functions:
2323
return fold_scopes(filtered_scopes)
2424
else:
@@ -105,24 +105,10 @@ def _get_nearest_block(
105105

106106

107107
def _filter_nocl_scopes(
108-
scopes: list[Scope], tokens: list[Token], nocl_comment_tokens: list[Token]
108+
scopes: list[Scope], nocl_comment_tokens: list[Token]
109109
) -> list[Scope]:
110110
nocl_comment_lines = [t.location.line for t in nocl_comment_tokens]
111-
112-
def get_scope_header_lines(scope: Scope) -> set[int]:
113-
header_token_range = scope.header.token_range
114-
header_tokens = tokens[header_token_range.start:header_token_range.end]
115-
result = set([t.location.line for t in header_tokens])
116-
first_line = header_tokens[0].location.line
117-
if first_line > 0:
118-
result.add(first_line - 1)
119-
return result
120-
121-
return [
122-
s
123-
for s in scopes
124-
if len(get_scope_header_lines(s).intersection(nocl_comment_lines)) == 0
125-
]
111+
return [s for s in scopes if s.header.name_token.location.line not in nocl_comment_lines]
126112

127113

128114
def has_name_prefix(tokens: list[Token], index: int) -> bool:
@@ -144,7 +130,7 @@ def get_headers(
144130
for pattern in patterns:
145131
name_token = next(t for t in pattern.tokens if t.is_name())
146132
if name_token:
147-
result.append(Header(name_token.value, TokenRange(pattern.start, pattern.end)))
133+
result.append(Header(name_token, TokenRange(pattern.start, pattern.end)))
148134
return result
149135

150136

codelimit/common/source_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ def filter_nocl_comment_tokens(tokens: list[Token]):
6565
def predicate(token: Token):
6666
if token.is_comment():
6767
value = token.value.lower()
68-
return value.startswith("#nocl") or value.startswith("# nocl")
68+
if value.startswith("#") or value.startswith(";"):
69+
value = value[1:].strip()
70+
elif value.startswith("//") or value.startswith("/*"):
71+
value = value[2:].strip()
72+
return value.startswith("nocl")
6973
else:
7074
return False
7175

tests/common/test_source_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pygments.lexers import PythonLexer
2+
from pygments.lexers import CppLexer
23

34
from codelimit.common.Location import Location
45
from codelimit.common.lexer_utils import lex
@@ -116,3 +117,16 @@ def test_filter_nocl_comments():
116117

117118
assert len(result) == 1
118119
assert result[0].location.line == 1
120+
121+
code = ""
122+
code += "// nocl\n"
123+
code += "void\n"
124+
code += "foo(Bar bar) {\n"
125+
code += " bar.foo();\n"
126+
code += "}\n"
127+
tokens = lex(CppLexer(), code, False)
128+
129+
result = filter_nocl_comment_tokens(tokens)
130+
131+
assert len(result) == 1
132+
assert result[0].location.line == 1

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def assert_units(code: str, language: Language, units: dict[str, int]):
1717
scopes = unfold_scopes(scopes)
1818
assert len(scopes) == len(units)
1919
for idx, scope in enumerate(scopes):
20-
assert scope.header.name in units
21-
assert count_lines(scope, code_tokens) == units[scope.header.name]
20+
assert scope.header.name() in units
21+
assert count_lines(scope, code_tokens) == units[scope.header.name()]
2222

2323

2424
def print_units(code: str, language: Language):

tests/languages/test_Cpp.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ class Main {
7272
"""
7373

7474
assert_units(code, Languages.Cpp, {"sayHello": 3})
75+
76+
77+
def test_skip_function_with_nocl_comment():
78+
code = """
79+
void foo(Bar bar) {
80+
bar.foo();
81+
}
82+
"""
83+
84+
assert_units(code, Languages.Cpp, {"foo": 3})
85+
86+
code = """
87+
void foo(Bar bar) { // nocl
88+
bar.foo();
89+
}
90+
"""
91+
92+
assert_units(code, Languages.Cpp, {})

tests/languages/test_Python.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -147,37 +147,19 @@ def foo(
147147

148148
def test_skip_function_with_nocl_comment_in_header():
149149
code = """
150-
def bar(
151-
bar: Bar
152-
) -> JSONResponse: # nocl
153-
bar = foo
154-
155-
def foo(
156-
foo: Foo
157-
) -> None:
158-
foo = bar
159-
bar = foo
160-
"""
161-
162-
assert_units(code, Languages.Python, {"foo": 5})
163-
164-
165-
def test_skip_function_with_nocl_comment_before_header():
166-
code = """
167-
def bar(
150+
def bar( # NOCL
168151
bar: Bar
169152
) -> JSONResponse:
170153
bar = foo
171154
172-
# NOCL
173155
def foo(
174156
foo: Foo
175157
) -> None:
176158
foo = bar
177159
bar = foo
178160
"""
179161

180-
assert_units(code, Languages.Python, {"bar": 4})
162+
assert_units(code, Languages.Python, {"foo": 5})
181163

182164

183165
def test_function_with_type_hints():

0 commit comments

Comments
 (0)