terryyin / lizard

A simple code complexity analyser without caring about the C/C++ header files or Java imports, supports most of the popular languages.
Other
1.82k stars 248 forks source link

support for blank lines & comments #334

Open dmctavish opened 2 years ago

dmctavish commented 2 years ago

Hi I was hoping it wouldn't be too much of a hassle to add support for blank lines as well as comment lines?

dmctavish commented 2 years ago

A couple of questions hopefully for @terryyin

I started tinkering, and thought I could model this after the bool extension, but I wanted confirmation that "comment" tokens aren't skipped over. my theory is: if I use the reader.get_comment_from_token() I can determine if we are in a "comment", and if so, increment the counter

One thing I'm also noting is that the Python language does not have support for multi-line comments within triple quotes - I'll raise that separately

''' This is an extension of lizard, that counts the number of comment lines '''

class LizardExtension(): # pylint: disable=R0903

FUNCTION_INFO = {"comment_count": {"caption": " comments "}}

def __call__(self, tokens, reader):
    if not hasattr(reader.context.current_function, "comment_count"):
        reader.context.current_function.comment_count = 0

    for token in tokens:
        comment = reader.get_comment_from_token(token)
        if comment is not None:
            reader.context.current_function.comment_count += 1
        yield token

def cross_file_process(self, fileinfos):
    '''
    Combine the statistics from each file.
    '''
    for fileinfo in fileinfos:
        if hasattr(fileinfo, "comment_count"):
            self.total_comments += fileinfo.comment_count
        yield fileinfo