Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the entire file matches a selector in is_typescript #705

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions typescript/libs/view_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,29 @@ def active_window():
"""Return currently active window"""
return sublime.active_window()

def selector_matches_whole_file(view, selector):
regions = view.find_by_selector(selector)
return len(regions) == 1 and regions[0].size() == view.size()

def is_typescript(view):
"""Test if the outer syntactic scope is 'source.ts' or 'source.tsx' """
if not view.file_name():
return False

try:
location = view.sel()[0].begin()
except:
return False

is_ts_file = view.match_selector(location, 'source.ts, source.tsx')
# Check if the *entire file* is one contiguous TypeScript/JavaScript region.
#
# Why am I writing this note? We used to test for the *current selection*.
# This meant that as soon as a user clicked into an html `<script>` tag,
# (whose scope was something like source.js), then this function
# would suddenly return True. The entire file would then be
# treated as a `.ts` or `.js` file and the user would be given red squiggles!
# Clearly we shouldn't try to parse a `.html` file as TypeScript.
is_ts_file = selector_matches_whole_file(view, "source.ts, source.tsx")
is_js_file = cli.enable_language_service_for_js \
and view.match_selector(location, 'source.js, source.jsx')
and selector_matches_whole_file(view, "source.js, source.jsx")

return is_ts_file or is_js_file


def is_special_view(view):
"""Determine if the current view is a special view.

Expand Down