give TypeScript higher priority than TypoScript

This commit is contained in:
Alan Hamlett 2017-02-20 13:22:14 -08:00
parent fad721530a
commit 4ea96210f8
5 changed files with 51 additions and 35 deletions

View file

@ -0,0 +1,3 @@
/**
* A Comment
*/

View file

@ -198,3 +198,19 @@ class LanguagesTestCase(utils.TestCase):
language = None
self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language)
def test_typescript_detected_correctly(self):
response = Response()
response.status_code = 500
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
entity = 'tests/samples/codefiles/typescript.ts'
args = ['--file', entity, '--config', config, '--time', now]
retval = execute(args)
self.assertEquals(retval, 102)
language = u('TypeScript')
self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language)

View file

@ -1,4 +0,0 @@
import tzlocal
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
from pygments.modeline import get_filetype_from_buffer
from pygments.util import ClassNotFound

View file

@ -450,6 +450,7 @@ class TypeScriptLexer(RegexLexer):
aliases = ['ts', 'typescript']
filenames = ['*.ts', '*.tsx']
mimetypes = ['text/x-typescript']
priority = 0.11
flags = re.DOTALL | re.MULTILINE

View file

@ -16,12 +16,12 @@ import sys
from .compat import u, open
from .dependencies import DependencyParser
from .packages import (
from .packages.pygments.lexers import (
ClassNotFound,
get_lexer_by_name,
guess_lexer_for_filename,
get_filetype_from_buffer,
)
from .packages.pygments.lexers import ClassNotFound
from .packages.pygments.modeline import get_filetype_from_buffer
try:
from .packages import simplejson as json # pragma: nocover
@ -32,6 +32,32 @@ except (ImportError, SyntaxError): # pragma: nocover
log = logging.getLogger('WakaTime')
def get_file_stats(file_name, entity_type='file', lineno=None, cursorpos=None,
plugin=None, alternate_language=None):
if entity_type != 'file':
stats = {
'language': None,
'dependencies': [],
'lines': None,
'lineno': lineno,
'cursorpos': cursorpos,
}
else:
language, lexer = guess_language(file_name)
parser = DependencyParser(file_name, lexer)
dependencies = parser.parse()
if language is None and alternate_language:
language = standardize_language(alternate_language, plugin)
stats = {
'language': language,
'dependencies': dependencies,
'lines': number_lines_in_file(file_name),
'lineno': lineno,
'cursorpos': cursorpos,
}
return stats
def guess_language(file_name):
"""Guess lexer and language for a file.
@ -79,13 +105,13 @@ def guess_lexer_using_filename(file_name, text):
try:
lexer = guess_lexer_for_filename(file_name, text)
except: # pragma: nocover
except:
pass
if lexer is not None:
try:
accuracy = lexer.analyse_text(text)
except: # pragma: nocover
except:
pass
return lexer, accuracy
@ -159,32 +185,6 @@ def number_lines_in_file(file_name):
return lines
def get_file_stats(file_name, entity_type='file', lineno=None, cursorpos=None,
plugin=None, alternate_language=None):
if entity_type != 'file':
stats = {
'language': None,
'dependencies': [],
'lines': None,
'lineno': lineno,
'cursorpos': cursorpos,
}
else:
language, lexer = guess_language(file_name)
parser = DependencyParser(file_name, lexer)
dependencies = parser.parse()
if language is None and alternate_language:
language = standardize_language(alternate_language, plugin)
stats = {
'language': language,
'dependencies': dependencies,
'lines': number_lines_in_file(file_name),
'lineno': lineno,
'cursorpos': cursorpos,
}
return stats
def standardize_language(language, plugin):
"""Maps a string to the equivalent Pygments language."""