-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcount_inline_comments.py
More file actions
52 lines (41 loc) · 1.64 KB
/
count_inline_comments.py
File metadata and controls
52 lines (41 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# this will count inline comments, which are lines that have both code and comments
# INLINE COMMENT LINE IS A LINE THAT HAS BOTH CODE AND A COMMENT
# so like: y = 5 #sets y to 5 IS AN INLINE COMMENT LINE!!!!!!!!
from utils.get_lexer import get_lexer_for_file
from lexers.token import TokenType
import os
def count_inline_comments(file_path):
"""Count lines that have both code and comments in a file.
Args:
file_path (str): Path to the file to analyze
Returns:
int: Number of lines that have both code and comments
"""
# Get the appropriate lexer for the file
Lexer = get_lexer_for_file(file_path)
# Read the file content
with open(file_path, 'r', encoding='utf-8') as f:
code = f.read()
# Initialize lexer with source code
lexer = Lexer(source_code=code)
# Get all tokens
tokens = lexer.tokenize()
# Group tokens by line number
tokens_by_line = {}
for token in tokens:
if token.line not in tokens_by_line:
tokens_by_line[token.line] = []
tokens_by_line[token.line].append(token)
# Count lines that have both code and comment tokens
inline_comment_count = 0
for line_num, line_tokens in tokens_by_line.items():
has_comment = False
has_code = False
for token in line_tokens:
if token.type == TokenType.COMMENT:
has_comment = True
elif token.type not in [TokenType.NEWLINE, TokenType.COMMENT]:
has_code = True
if has_comment and has_code:
inline_comment_count += 1
return inline_comment_count