Sublime Forum

APIs to manipulate projects?

#1

I want to programatically mess with projects. For this, the first two items I need are a way to iterate through all files in a window’s project, a way to extract the data for a given sublime-project file.and a way to provide a list of paths and have a sublime-project file built from that.

Are there any undocumented APIs that could help with any of this? Any method defined in the default packages or third party packages in Package Control I could look at for this?

0 Likes

#2

why are you only interested in undocumented APIs? you never seem to bother checking the official API reference, where you’d find:

  • folders() Returns a list of the currently open folders.
  • project_file_name() Returns name of the currently opened project file, if any.
  • project_data() Returns the project data associated with the current window. The data is in the same format as the contents of a .sublime-project file.
  • set_project_data(data) Updates the project data associated with the current window. If the window is associated with a .sublime-project file, the project file will be updated on disk, otherwise the window will store the data internally.

as for iterating through all the files, this currently has to be done manually

1 Like

#3

Sorry if I was implying otherwise - I know of the official API reference, I’m taking it for granted, I am only interested in undocumented APIs because I have no easily access to/knowledge of.

That being said, I hadn’t noticed the folders() method. And not that I look at it, I’m not sure exactly what it does - what is an “open folder” in the context of Sublime Text 3? From wuickly running it on the console, it seems to just return the top-level folders in the window’s project, is that description correct?

If folders does that, that’d be a godsend. Otherwise I’d need to build the list of files/folders from the project_data() (that is my backup plan if I don’t find any plugin/package that already has this functionality).

0 Likes

#4

Yes, open folders are the top level folders that are added to the project; specifically it equates to all of the entries in the folders key in the sublime-project file, but generically speaking even in a window with no explicitly opened project there is still a list of folders that are open (i.e. because you dragged one or more into the window).

The ${folder} item in build systems and such refers to the first folder in the list (i.e. the top folder in the sidebar).

0 Likes

#5

There is something I use for my variable extender which is part of sublime… it gives you the exact path to the folder name… From there you could use os library functions to recurse through all folders and grab a list of all targeted files and then you could read them, extract the data and compile it to a file for output or to a view or however you want…

However - depending on EXACTLY what you’re trying to design - there may be a much FASTER / more efficient way to do something like this … Sublime Text indexes your projects which is why I’m asking for the specific task you’re trying to perform…

sublime.active_window( ).extract_variables( ) returns a Dictionary such as folder, etc… as keys which gives you information about the project / session the current window has loaded… But also other information such as the active view filename, and more…

Just be advised - most of the session / project keys are named properly but folder is part of the session / project data but it looks more like it’s part of the active file…

0 Likes

#6

Can you explain what you mean by this, because as I said above ${folder} is the first top level folder added to the sidebar, regardless of the file that you have open:

Note that even though a file from Folder B is open, the result of folder is the folder that’s at the top of the Sidebar, which is also the folder that’s first in the folders key. This holds true even if you have no files open or if you have a file open that’s not contained in any of the folders in the sidebar.

0 Likes

#7

Here’s what I’m trying to do:

I have a domain specific language I’m creating support for. The problem with it is that it uses different syntaxes depending on where the file is located. so a txt file inside the gfx folder will have a different syntax than a .txt file inside the scripts folder, etc. While I can work around this WRT giving the appropriate syntax to the files when they open, it means I can’t give appropiate support for symbols, go to definition, etc. since that relies, AFAIK, on applying a syntax just based on file extension and first like matching.

So what I want is to have it exposed to my commands a tree of all folders and text files within each folder, so I can do generic operations with them (e.g. parsin them and creating a custom index of symbols to create a surrogate Go to definition feature).

You mention Sublime indexing projects. How can I access this indexing?

0 Likes