Skip to content

Commit

Permalink
use backward compatible tokenizer func
Browse files Browse the repository at this point in the history
  • Loading branch information
youben11 committed Nov 15, 2019
1 parent 0a9d163 commit 226dc59
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions pyls/plugins/importmagic_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import sys
import tokenize
from io import BytesIO
from io import StringIO
from concurrent.futures import ThreadPoolExecutor
import importmagic
from pyls import hookimpl, lsp, _utils
Expand Down Expand Up @@ -66,12 +66,11 @@ def _get_imports_list(source, index=None):

def _tokenize(source):
"""Tokenize python source code.
Returns only NAME tokens.
"""
stream = BytesIO(source.encode())
tokens = tokenize.tokenize(stream.readline)
if tokens is None:
return []
return list(tokens)
readline = StringIO(source).readline
filter_name = lambda token: token[0] == tokenize.NAME
return filter(filter_name, tokenize.tokenize(readline))


def _search_symbol(source, symbol):
Expand All @@ -94,8 +93,8 @@ def _search_symbol(source, symbol):
}
}
"""
symbol_tokens = _tokenize(symbol)
source_tokens = _tokenize(source)
symbol_tokens = list(_tokenize(symbol))
source_tokens = list(_tokenize(source))

get_str = lambda token: token[1]
symbol_tokens_str = list(map(get_str, symbol_tokens))
Expand Down

0 comments on commit 226dc59

Please sign in to comment.