Sublime Forum

Open specific file type in several tabs

#1

I need a plugin to open an uncommon file type, parse it to a few parts and collect those parts back on “Save”.

For example, opening program_code.c

//INCLUDE
include SPI.h
//INCLUDE_END
//SETTINGS
void setup()
{
   SPI.begin(); 
}
//SETTINGS_END
//MAIN LOOP
void loop()
{
   SPI.transfer(0xdead);
}
//MAIN LOOP_END

and parsing “INCLUDE”, “SETTINGS”, “MAIN LOOP” sections results in opening three tabs:

1st tab

    #include SPI.h

2nd tab

void setup()
{
SPI.begin(); 
}

3rd tab

void loop()
{
SPI.transfer(0xdead);
}

Are there any ways to make it work? Or any examples?

0 Likes

#2

How about this?

import sublime_plugin
import sublime


class DissectThisFileCommand(sublime_plugin.TextCommand):

    def run(self, _, tokens=["INCLUDE", "SETTINGS", "MAIN LOOP"]):
        comment_regions = self.view.find_by_selector("comment.line")
        self.active_token = None
        self.start = 0
        self.end = 0
        self.tokens = tokens
        for multiline_regions in comment_regions:
            regions = self.view.split_by_newlines(multiline_regions)
            for region in regions:
                self._handle_region(region)

    def _handle_region(self, region):
        comment = self.view.substr(region).strip()
        if self.active_token:
            if comment == "//{}_END".format(self.active_token):
                view = self.view.window().new_file()
                view.settings().set("syntax",
                                    self.view.settings().get("syntax"))
                view.set_name("{}_{}".format(self.view.name(),
                                             self.active_token))
                characters = self.view.substr(sublime.Region(self.start,
                                                             region.a - 1))
                view.run_command("append", {"characters": characters})
                self.active_token = None
        else:
            for token in self.tokens:
                if comment == "//{}".format(token):
                    self.start = region.b + 1
                    self.active_token = token
                    break

Go to Tools -> Developer -> New Plugin and save this python script in your Packages/User directory.

To run it, open your C file, open the console, and type this:

>>> view.run_command("dissect_this_file")

(or bind it to a keybinding, or put it in the command palette, or …)

2 Likes

#3

wow, thanks for you help. i guess, i’ll google all the rest myself if there are any bugs

1 Like

#4

I wonder, is it possible to create a new View into a file instead of creating a new file self.view.window().new_file().
It would be much better to have direct access to the region between comment lines without creating a file. I guess that SublimeText itself supports creating a bunch of separate views into one file, but does it support having several views into regions of a file?

0 Likes

#5

Not by default, no. I guess plugin support for this could and probably has been added, but I don’t know how good the user experience would be.

0 Likes

#6

Let me make it clear, actually, there were two questions:

  1. Is there a way to make multiple views into one file but different regions? – answer is ‘No’
  2. Can I create a View without creating a file?
0 Likes

#7

Yes; calling window.new_file() creates a new view, but despite the name it’s not backed by any file until it’s saved for the first time; essentially this is what happens when you select File > New File from the menu.

By default a view that’s closed will prompt you to save any changes, and will appear as modified if you have the appropriate options set. If your intention is to make a view that’s never going to be associated with a file, you should set it to be a scratch file as well:

import sublime
import sublime_plugin


class NoFileViewCommand(sublime_plugin.WindowCommand):
    def run(self, caption="No File Loaded"):
        view = self.window.new_file()
        view.set_scratch(True)
        view.set_name(caption)
3 Likes

#8

Thanks for the explanation, I’ll try it

0 Likes