Last active
August 29, 2015 14:26
-
-
Save blink1073/2cc5185d247d803540d2 to your computer and use it in GitHub Desktop.
Token Completions Using Pygments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pygments.lexers import get_lexer_for_filename | |
| from pygments.token import Token | |
| def get_matches(code, filename): | |
| """Get token-based matches for the current context in a file. | |
| """ | |
| lexer = get_lexer_for_filename(filename) | |
| tokens = [i for i in lexer.get_tokens(code)] | |
| # Handle opening strings (which generate an error) | |
| if len(tokens) > 3: | |
| if tokens[-3][0] in Token.Error and tokens[-3][1] in ['"', "'"]: | |
| code += tokens[-3][1] | |
| tokens = [i for i in lexer.get_tokens(code)] | |
| tokens = tokens[:-1] # throw out the trailing newline | |
| context, obj = tokens[-1] | |
| if context in Token.Literal.String: | |
| context = Token.Literal.String | |
| tokens = [i[1] for i in tokens[:-1] if i[0] in context] | |
| if context not in Token.Literal.String: | |
| tokens += list(lexer.tokens['keywords'][0][0].words) | |
| tokens = [i for i in tokens if obj in i] | |
| return [t[t.index(obj):].split()[0] for t in tokens] | |
| if __name__ == '__main__': | |
| tokens = get_matches('''b = 'hi there hello'; a = "hell''', 'test.py') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment