yoshizow / global-pygments-plugin

[DEPRECATED] Pygments Plug-in Parser for GNU GLOBAL
Other
66 stars 16 forks source link

pygments_parser.py exception: KeyError: '.Bat' #12

Open skywind3000 opened 6 years ago

skywind3000 commented 6 years ago

I am here because found your name in the head of pygments_parser.py, and google tells me you have a project here.

Running gnu global 6.6.2 on windows with pygments_parser.py, I encountered this:

E:\Lesson\lesson>gtags
Traceback (most recent call last):
  File "d:\acm\github\support\tools\gtags\bin\../share/gtags/script/pygments_parser.py", line 259, in <module>
    main()
  File "d:\acm\github\support\tools\gtags\bin\../share/gtags/script/pygments_parser.py", line 256, in main
    handle_requests(langmap, parser_options)
  File "d:\acm\github\support\tools\gtags\bin\../share/gtags/script/pygments_parser.py", line 218, in handle_requests
    tags = parser.parse(path)
  File "d:\acm\github\support\tools\gtags\bin\../share/gtags/script/pygments_parser.py", line 175, in parse
    ref_result = self.ref_parser.parse(path)
  File "d:\acm\github\support\tools\gtags\bin\../share/gtags/script/pygments_parser.py", line 104, in parse
    lexer = self.get_lexer_by_langmap(path)
  File "d:\acm\github\support\tools\gtags\bin\../share/gtags/script/pygments_parser.py", line 114, in get_lexer_by_langmap
    lang = self.langmap[ext]
KeyError: '.Bat'
gtags: unexpected EOF.

after looking into pygments_parser.py, I found:

    def get_lexer_by_langmap(self, path):
        ext = os.path.splitext(path)[1]
 ->     lang = self.langmap[ext]
        if lang:
            name = lang.lower()
            if name in LANGUAGE_ALIASES:
                name = LANGUAGE_ALIASES[name]
            lexer = pygments.lexers.get_lexer_by_name(name)
            return lexer
        return None

the third line cannot handle extnames which don't exist in the langmap, and should be fixed as:

-     lang = self.langmap[ext]
+     lang = self.langmap.get(ext, None)

same problem in pygments_parser.py.in:

https://github.com/yoshizow/global-pygments-plugin/blob/aebed6d2c77c01c82319aa6577c3b51c57e20ecc/pygments_parser.py.in#L105-L110

skywind3000 commented 6 years ago

Would you please make a patch to pygments_parser.py in gnu global after fixing this ?

skywind3000 commented 6 years ago

It can be reproduced easily on Windows, just put an empty "hello.Bat" (keep the first letter of .bat upcase) file in your project, than run gtags with pygments label enabled.

skywind3000 commented 6 years ago

I believe, gtags will lower case ext-names on windows, but pygments_parser.py won't, that works fine on linux but will crash on windows when you got a .C, .Cpp, .Py or .Bat filename.