ternjs / tern_for_sublime

Sublime Text package adding Tern support
MIT License
803 stars 54 forks source link

Customizable scopes for completions #3

Closed subhaze closed 11 years ago

subhaze commented 11 years ago

I guess this is more of an enhancement request but it would be nice to allow completions within <script/> when editing HTML files.

Another plus would be having the ability to turn of completions when editing within quotes.

I can send a PR if you're interested.

marijnh commented 11 years ago

If there's an easy way in ST to find the range spanned by a script tag, I'd be interested in a pull request. If it would involve a horrible mess of regexps looking for <script> tags, I'd rather just find a way to turn Tern off in mixed-mode files (files that aren't all JS).

subhaze commented 11 years ago

As far as I can tell just adjusting where the scope is checked when determining if it's a JS file or not should do the trick for mixed-mode. I've tested this briefly and all seems well, though, I am ignorant to the internal workings of the tern server so I'm not sure if changing this will have unintended effects.

https://github.com/subhaze/tern_for_sublime/commit/1b63ebb9d0a6beba4659ebfcd391527e8fc420ef

marijnh commented 11 years ago

The problem with that is that the plugin will still send the whole buffer (or an undifferentiated slice of it if it's big) to the server, and the server will get confused by the HTML tags in the text. Is there an efficient way to find the start/end of a block that has a certain selector?

subhaze commented 11 years ago

I figured it wouldn't be that easy haha.

But yes, we could use the find_by_selector to gather up all the js scopes within the current buffer. At the moment I don't have time to code a prototype out but should have some time this evening to look more into this.

The following should get what is needed though.


# print out list of region positions
print(view.find_by_selector('source.js.embedded.html'))

# loop through and print out content of each region found
for region in view.find_by_selector('source.js.embedded.html'):
  print(view.substr(region))
marijnh commented 11 years ago

find_by_selector appears to be exactly what I needed. Try with patch 219cdcfa80a4cac13afecd5d0d372db063b7e10a

subhaze commented 11 years ago

After updating line 11 to return view.score_selector(view.sel()[0].b, "source.js") > 0, so that tern is triggered in mixed-mode files, there's an error being thrown.

error: Error: Position 893 is outside of file. which seems odd because that is a valid point the file.

marijnh commented 11 years ago

Right, I messed up the positions. Worked around it in 7584b7080e314bb9e2dd7f220cdf3bce5d38f2c4