Home Download Buy Blog Forum Support

Dev Build 2030

Re: Dev Build 2030

Postby asmodai on Sun Feb 27, 2011 5:12 pm

Jon, I cannot get what you wrote to work.

I created a Headers (C).sublime-completions with (shortened) the following contents:
Code: Select all
{
   "scope": "string.quoted.other.lt-gt.include.c meta.preprocessor.c.include source.c",

   "completions":
   [
      "assert.h",
      "complex.h"
   ]
}

I cannot get this to work. If I take out the parts in scope before "source.c" then I can get it to complete. I have set the syntax explicitly to C in the View menu. So what obvious thing am I doing wrong here? It should be similar to your C++ example. I've tried "meta.preprocessor.c.include source.c" as well as "string.quoted.other.lt-gt.include.c source.c" as well, but they also failed.

Also, my question before was actually different. Since I communicated the idea across badly, let me try again. Within the scope of the source file if you would allow all completion tokens it would be incredibly bloated. So depending on which #include files are included at the top I would like to limit the tokens. E.g. if #include <stdio.h> is included I want fclose() to be available in autocompletion, but if #include <stdio.h> is not present, I don't want to offer the possibility.
asmodai
 
Posts: 73
Joined: Mon Mar 01, 2010 10:42 am

Re: Dev Build 2030

Postby adzenith on Sun Feb 27, 2011 6:14 pm

asmodai wrote:E.g. if #include <stdio.h> is included I want fclose() to be available in autocompletion, but if #include <stdio.h> is not present, I don't want to offer the possibility.


I think a plugin could be what you want here. Using Jon's example plugin as a starting point:
Code: Select all
import sublime
import sublime_plugin

class C_Completions(sublime_plugin.EventListener):
  all_completions = {"string.h":
                      [("sl", "strlen($1)"),
                         ],
                     "stdio.h":
                      [("pf", "printf($1)"),
                       ]}
 
  def on_query_completions(self, view, prefix, locations):
    if not view.match_selector(locations[0], "source.c"):
        return []
    filenames = []
    regions = view.find_all(r"#include +[\"<](.+)[\">]", 0, "$1", filenames)
    completions = []
    for f,r in zip(filenames, regions):
      if (f in self.all_completions and
          view.syntax_name(r.begin()).strip().endswith("preprocessor.c.include source.c")):
        completions += self.all_completions[f]
    return completions
adzenith
 
Posts: 1216
Joined: Mon Oct 19, 2009 9:12 pm

Re: Dev Build 2030

Postby atomi on Sun Feb 27, 2011 8:33 pm

Is there a way to get the scope name in a plugin

I tried
Code: Select all
view.run_command('show_scope_name')

and theres view.scope_name() but I can't tell what args it takes or even if it does what i need (grab the the file source type eg: source.css.less) so i can check against it.

We really really need some api docs for ST2.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Dev Build 2030

Postby adzenith on Sun Feb 27, 2011 9:21 pm

I believe view.syntax_name and view.scope_name are identical. They both take a text_point as an argument and return a string containing the name of the scope at the text point.
If a function exists in both ST1 and ST2, it tends to take the same arguments and have the same effect, so the API docs for ST1 are actually pretty helpful.
adzenith
 
Posts: 1216
Joined: Mon Oct 19, 2009 9:12 pm

Re: Dev Build 2030

Postby atomi on Sun Feb 27, 2011 9:40 pm

adzenith thanks for the quick reply.

this seems to work
Code: Select all
view.scope_name(view.text_point(0,0)).split(' ')[-2]
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Dev Build 2030

Postby adzenith on Sun Feb 27, 2011 9:44 pm

atomi wrote:adzenith thanks for the quick reply.

this seems to work
Code: Select all
view.scope_name(view.text_point(0,0)).split(' ')[-2]

view.text_point(0,0) will always just be 0, I believe.
You can also call view.settings().get("syntax") in order to figure out which syntax file is being used.
adzenith
 
Posts: 1216
Joined: Mon Oct 19, 2009 9:12 pm

Re: Dev Build 2030

Postby atomi on Sun Feb 27, 2011 9:56 pm

view.text_point(0,0) will always just be 0, I believe.


I don't understand... "will always just be 0"
Sorry.

Are you talking about the return value?
I'm getting as the return value fwiw punctuation.definition...
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Dev Build 2030

Postby adzenith on Sun Feb 27, 2011 10:02 pm

view.text_point() returns a a text_point, which is really just an integer index of the indicated character in the buffer. Because the character at row 0, col 0 is always the character at buffer index 0, view.text_point(0,0) will always return 0.
Let me know if I'm not making any sense.
adzenith
 
Posts: 1216
Joined: Mon Oct 19, 2009 9:12 pm

Re: Dev Build 2030

Postby atomi on Sun Feb 27, 2011 10:14 pm

Oh I understand now.
So,
Code: Select all
view.scope_name(0)

would work just the same.

Thanks again.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Dev Build 2030

Postby adzenith on Sun Feb 27, 2011 10:38 pm

Right, yeah, I should have just said that. :\
Good luck with your plugin!
adzenith
 
Posts: 1216
Joined: Mon Oct 19, 2009 9:12 pm

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: Astrophizz, demetris, Exabot [Bot] and 43 guests