Sublime Forum

Is it possible to open a file without showing it to the user?

#1

A bit of background on that weird question on the thread title:

I’ve written a hhighlighter for a game’s scripting language - a scripting language which is pretty verbore and thus pretty easy for modders to make mistakes in).
I want to write a command that does this: iterate over all files in the current project. If the file are part of this scripting language, parse the file and retrieve the position of the regions parsed as invalid.illegal, then show the list of said regions’ positions to the user. For this, I’d rather not have the user seeing tabs opening and closing while the process is underway.

Is something like this possible? Is it sensible or should I write my own parser outside of Sublime? That’d seem redundant seeing as how sublime can do the lexing by itself, and in a more efficient way that I could with python scripts too.

0 Likes

#2

Most (if not all) similar systems use external analysis tools. If such a tool existed, it would probably be easy to plug it into the SublimeLinter framework. Sublime isn’t really designed for this; its parser is designed to efficiently highlight views, and it’s not easy to repurpose it for other applications.

0 Likes

#3

Panel views (such as the output from a build system) are just regular view objects that you can work with the same as a regular file view, and you can create them as unlisted which stops them from being visible to the user in the panel switcher dialog. As such you can do anything you want in that view and nobody will be able to see it.

I agree with @ThomSmith in that if you have any serious linting to do it’s probably best done in some external tool for the reasons that he mentioned. On the other hand if you have very simple linting needs it may make sense to do something like that in Sublime, although I’m probably saying this because I do that very thing in something I’m working on currently.

In my specific case (a help system) I’m doing the linting directly from within Sublime for the following reasons:

  • All of the information I need can be gleaned by looking for specific scopes in files
  • I expect that most or all files subject to being linted will already be open
  • Having Sublime drive the lint means that there are no external tools needed
  • I’m exceedingly lazy and busy (mostly lazy) and would have to write an external parser

An example of creating an unlisted panel view for doing something like this is available here. Given a filename it first checks all windows to see if the file is already open or not. If it’s not is manually loads it into a hidden panel view and applies the appropriate syntax to it.

2 Likes

#4

If you want to parse the entire project - use an external system…

If you want to parse the file as the file is opened then I’d suggest using something pre-built ( building a standalone plugin to open a new panel, which can be seen or which isn’t but saves to a file or something, is a pain - but not impossible )… like Code Map - or… my stand-alone plugin similar to CodeMap with a ton more features will be out very soon…

CodeMap lets you process a file line by line and then return text to categorize it ( but since it has no category system, if you see an identical category twice with some other in between then the output shows 3 )… My plugin used CodeMap as a base but I didn’t like the limitations - I added a category system and a bunch of other features so you didn’t need to add 100 lines just to capture a single line or type of line, etc…

I decided to go stand-alone a while back and it’s almost ready for release with a ton of new features such as processing the file line by line, or by chunking it, and / or using sublime built in features to make universal mapping - mapping based on projects, etc… much more robust and the categories are actually categories so there are no duplicates…

If you don’t need to create a panel, then the plugin becomes a LOT easier to write… On activated async ( use async otherwise main ui thread will lock up ) check if the currently active view in the active window has the file extension you want, and check other data before reading either the view contents, or by loading the file into memory… Reading the contents of the view would be quicker / easier and less code…

0 Likes