Show More
@@ -13,6 +13,8 b' class CompletionLexer(object):' | |||
|
13 | 13 | 'Python' : [ '.' ] } |
|
14 | 14 | |
|
15 | 15 | def __init__(self, lexer): |
|
16 | """ Create a CompletionLexer using the specified Pygments lexer. | |
|
17 | """ | |
|
16 | 18 | self.lexer = lexer |
|
17 | 19 | |
|
18 | 20 | def get_context(self, string): |
@@ -23,21 +25,36 b' class CompletionLexer(object):' | |||
|
23 | 25 | reversed_tokens = list(self._lexer.get_tokens(string)) |
|
24 | 26 | reversed_tokens.reverse() |
|
25 | 27 | |
|
26 | # Pygments often tacks on a newline when none is specified in the input | |
|
28 | # Pygments often tacks on a newline when none is specified in the input. | |
|
29 | # Remove this newline. | |
|
27 | 30 | if reversed_tokens and reversed_tokens[0][1].endswith('\n') and \ |
|
28 | 31 | not string.endswith('\n'): |
|
29 | 32 | reversed_tokens.pop(0) |
|
30 | 33 | |
|
31 | 34 | current_op = unicode() |
|
32 | 35 | for token, text in reversed_tokens: |
|
33 | if is_token_subtype(token, Token.Name) and \ | |
|
34 | (not context or current_op in self._name_separators): | |
|
35 | if not context and current_op in self._name_separators: | |
|
36 | ||
|
37 | if is_token_subtype(token, Token.Name): | |
|
38 | ||
|
39 | # Handle a trailing separator, e.g 'foo.bar.' | |
|
40 | if current_op in self._name_separators: | |
|
41 | if not context: | |
|
36 | 42 | context.insert(0, unicode()) |
|
43 | ||
|
44 | # Handle non-separator operators and punction. | |
|
45 | elif current_op: | |
|
46 | break | |
|
47 | ||
|
37 | 48 | context.insert(0, text) |
|
38 | 49 | current_op = unicode() |
|
50 | ||
|
51 | # Pygments doesn't understand that, e.g., '->' is a single operator | |
|
52 | # in C++. This is why we have to build up an operator from | |
|
53 | # potentially several tokens. | |
|
39 | 54 | elif token is Token.Operator or token is Token.Punctuation: |
|
40 | 55 | current_op = text + current_op |
|
56 | ||
|
57 | # Break on anything that is not a Operator, Punctuation, or Name. | |
|
41 | 58 | else: |
|
42 | 59 | break |
|
43 | 60 |
@@ -15,14 +15,23 b' class TestCompletionLexer(unittest.TestCase):' | |||
|
15 | 15 | """ |
|
16 | 16 | lexer = CompletionLexer(PythonLexer()) |
|
17 | 17 | |
|
18 | # Test simplest case. | |
|
18 | 19 | self.assertEquals(lexer.get_context("foo.bar.baz"), |
|
19 | 20 | [ "foo", "bar", "baz" ]) |
|
21 | ||
|
22 | # Test trailing period. | |
|
20 | 23 | self.assertEquals(lexer.get_context("foo.bar."), [ "foo", "bar", "" ]) |
|
21 | 24 | |
|
25 | # Test with prompt present. | |
|
22 | 26 | self.assertEquals(lexer.get_context(">>> foo.bar.baz"), |
|
23 | 27 | [ "foo", "bar", "baz" ]) |
|
28 | ||
|
29 | # Test spacing in name. | |
|
24 | 30 | self.assertEquals(lexer.get_context("foo.bar. baz"), [ "baz" ]) |
|
25 | 31 | |
|
32 | # Test parenthesis. | |
|
33 | self.assertEquals(lexer.get_context("foo("), []) | |
|
34 | ||
|
26 | 35 | def testC(self): |
|
27 | 36 | """ Does the CompletionLexer work for C/C++? |
|
28 | 37 | """ |
General Comments 0
You need to be logged in to leave comments.
Login now