give TypeScript higher priority than TypoScript
This commit is contained in:
parent
fad721530a
commit
4ea96210f8
5 changed files with 51 additions and 35 deletions
3
tests/samples/codefiles/typescript.ts
Normal file
3
tests/samples/codefiles/typescript.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
/**
|
||||||
|
* A Comment
|
||||||
|
*/
|
|
@ -198,3 +198,19 @@ class LanguagesTestCase(utils.TestCase):
|
||||||
|
|
||||||
language = None
|
language = None
|
||||||
self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language)
|
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)
|
||||||
|
|
|
@ -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
|
|
|
@ -450,6 +450,7 @@ class TypeScriptLexer(RegexLexer):
|
||||||
aliases = ['ts', 'typescript']
|
aliases = ['ts', 'typescript']
|
||||||
filenames = ['*.ts', '*.tsx']
|
filenames = ['*.ts', '*.tsx']
|
||||||
mimetypes = ['text/x-typescript']
|
mimetypes = ['text/x-typescript']
|
||||||
|
priority = 0.11
|
||||||
|
|
||||||
flags = re.DOTALL | re.MULTILINE
|
flags = re.DOTALL | re.MULTILINE
|
||||||
|
|
||||||
|
|
|
@ -16,12 +16,12 @@ import sys
|
||||||
from .compat import u, open
|
from .compat import u, open
|
||||||
from .dependencies import DependencyParser
|
from .dependencies import DependencyParser
|
||||||
|
|
||||||
from .packages import (
|
from .packages.pygments.lexers import (
|
||||||
|
ClassNotFound,
|
||||||
get_lexer_by_name,
|
get_lexer_by_name,
|
||||||
guess_lexer_for_filename,
|
guess_lexer_for_filename,
|
||||||
get_filetype_from_buffer,
|
|
||||||
)
|
)
|
||||||
from .packages.pygments.lexers import ClassNotFound
|
from .packages.pygments.modeline import get_filetype_from_buffer
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from .packages import simplejson as json # pragma: nocover
|
from .packages import simplejson as json # pragma: nocover
|
||||||
|
@ -32,6 +32,32 @@ except (ImportError, SyntaxError): # pragma: nocover
|
||||||
log = logging.getLogger('WakaTime')
|
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):
|
def guess_language(file_name):
|
||||||
"""Guess lexer and language for a file.
|
"""Guess lexer and language for a file.
|
||||||
|
|
||||||
|
@ -79,13 +105,13 @@ def guess_lexer_using_filename(file_name, text):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
lexer = guess_lexer_for_filename(file_name, text)
|
lexer = guess_lexer_for_filename(file_name, text)
|
||||||
except: # pragma: nocover
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if lexer is not None:
|
if lexer is not None:
|
||||||
try:
|
try:
|
||||||
accuracy = lexer.analyse_text(text)
|
accuracy = lexer.analyse_text(text)
|
||||||
except: # pragma: nocover
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return lexer, accuracy
|
return lexer, accuracy
|
||||||
|
@ -159,32 +185,6 @@ def number_lines_in_file(file_name):
|
||||||
return lines
|
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):
|
def standardize_language(language, plugin):
|
||||||
"""Maps a string to the equivalent Pygments language."""
|
"""Maps a string to the equivalent Pygments language."""
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue