summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Hammond <christian@beanbaginc.com>2017-01-22 11:35:31 -0800
committerChristian Hammond <christian@beanbaginc.com>2017-01-22 11:35:31 -0800
commitb55bc4bf9bec1e3454d38f5c3dc103ffd9fdc75d (patch)
treed1cc8c5ec3d14abd9a3d87d2a6cc2218b0ab27a4
parentf7aac3cab93586036b66c7a05f6a33db96bfb1b1 (diff)
downloadpygments-b55bc4bf9bec1e3454d38f5c3dc103ffd9fdc75d.tar.gz
Implement guessing and resolution between TypeScript and TypoScript.
The new TypoScript lexer was taking precedence over TypeScript, which share the same file extension. This meant that any previously-working TypeScript source files would be misidentified and highlighted incorrectly. This was due to the lack of a guesser for TypeScript, and TypoScript having a naturally higher priority. This change balances things a bit between the lexers. TypoScript now has an explicitly-higher priority than TypeScript, both for the file extension and defaults when guessing content, as it has less we can rely on in terms of content. TypeScript has a lower priority, but has an analyse_text() method that checks for common symbols in code, giving it a better chance of matching for actual TypeScript source files. TypeScript also gains an additional file extension, *.tsx. Ideally, this would have its own lexer that understood inline JSX syntax, but giving general TypeScript syntax highlighting for these files is a step in the right direction.
-rw-r--r--pygments/lexers/javascript.py12
-rw-r--r--pygments/lexers/typoscript.py7
2 files changed, 18 insertions, 1 deletions
diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py
index 4b47b15a..76b226c2 100644
--- a/pygments/lexers/javascript.py
+++ b/pygments/lexers/javascript.py
@@ -447,7 +447,7 @@ class TypeScriptLexer(RegexLexer):
name = 'TypeScript'
aliases = ['ts', 'typescript']
- filenames = ['*.ts']
+ filenames = ['*.ts', '*.tsx']
mimetypes = ['text/x-typescript']
flags = re.DOTALL | re.MULTILINE
@@ -516,6 +516,16 @@ class TypeScriptLexer(RegexLexer):
]
}
+ def analyse_text(text):
+ if re.search('^(import.+(from\s+)?["\']|'
+ '(export)?\s*(interface|class|function)\s+)',
+ text, re.MULTILINE):
+ return 1.0
+ else:
+ # Slightly lower than TypoScript, since we have more we can look
+ # for in TypeScript content.
+ return 0.8
+
class LassoLexer(RegexLexer):
"""
diff --git a/pygments/lexers/typoscript.py b/pygments/lexers/typoscript.py
index 407847ed..d1f09d90 100644
--- a/pygments/lexers/typoscript.py
+++ b/pygments/lexers/typoscript.py
@@ -115,6 +115,9 @@ class TypoScriptLexer(RegexLexer):
flags = re.DOTALL | re.MULTILINE
+ # Slightly higher than TypeScript (which is 0).
+ priority = 0.1
+
tokens = {
'root': [
include('comment'),
@@ -223,3 +226,7 @@ class TypoScriptLexer(RegexLexer):
def analyse_text(text):
if '<INCLUDE_TYPOSCRIPT:' in text:
return 1.0
+ else:
+ # Slightly higher than TypeScript, since there's less to rely
+ # on in TypoScript content.
+ return 0.9