Sublime Forum

Plugin which open file in subfolder

#1

Hi,
I need plugin which open file in subfolder of my current file. Let me explain that a bit more. I have this tree structure.

-2633568be4ce4887a29e8a4d19643a67 --subfile.vm -456789 --whatever.vm -789789 --againwhatever.vm myworkingfile.xml

I want to work in myworkingfile.xml where I have unique identifier which looks like: instanceIdentifier=“2633568be4ce4887a29e8a4d19643a67” and in this XML I have links to subfiles in folder named exactly same as my unique identifier. I want plugin which open my subfile.vm after clicking or right clicking on highligted text or something likethis - in $core_v2_widget.ExecuteFile(‘subfile.vm’) - so it has to get instanceIdentifier firts, than open that folder, that find file and than open it in new tab.

Do you know about plugin which provide this behavior?
Do I have to write it by myself?

thx for answers
Is there

0 Likes

#2

I believe you would have to create this on your own. Where your use-case is so specific and you are you driving it off of a specific attribute of the eml element, I don’t think there would be something currently available that would meet your needs. If you post a sample of the XML that is generated I may be able to mock something up quickly, but I don’t have enough information to get started with what you’ve presented.

Best,
Nick

0 Likes

#3

Hi, sure thing, here is my code Dashboard.xml file:

[code]


<![CDATA[

    $core_v2_widget.ExecuteFile('dashboard.vm')
  ]]>[/code]

and when you double/triple/whatever click on “dashboard.vm” I want to open file with name “dashboard.vm” in subfolder named “1a6ecf67e36d4040b45d6d5c947eb2f8”

One picture is better than 1000 words - screenshot of filetree here

Thank you in advance for your time

0 Likes

#4

Create a new file in the Packages/User directory and call it open_vm.py or something like that. Paste the following code into that file.

import sublime, sublime_plugin, os

class OpenInstanceVmFileCommand(sublime_plugin.WindowCommand):
	def run(self):
		#for each selection execute open file
		for sel in self.window.active_view().sel():
			# select the region for the file name
			# this will be used to locate the correct
			# instanceIdentifier
			target = self.get_target_file_region(sel)

			# skip if we didn't find a match
			if(not target):
				continue

			#extract the target file name
			target_file = self.window.active_view().substr(target).split("\'")[1]

			#get the identifier for this xml entry
			identifier = self.get_instance_identifier(target)

			self.open_vm_file(identifier, target_file)

	def open_vm_file(self, identifier, target_file):
		# I was getting weird results with this comment code, it may be needed for you.
		## get the xml file path
		## file_loc = os.path.dirname(self.window.active_view().file_name())
		## file_path = file_loc + os.sep + identifier + os.sep + target_file
		file_path = identifier + os.sep + target_file
		print (file_path)
		self.window.run_command("open_file", {"file": file_path})


	def get_instance_identifier(self, target):
		instanceIdentifiers = self.window.active_view().find_all("instanceIdentifier\=\"[a-z0-9]+")

		#locate the instanceId prior to this line
		for iid in reversed(instanceIdentifiers):
			# the current iid region is lower in the file so skip it
			if (iid.begin() > target.begin()):
				continue
			#we found a match so return it
			return self.window.active_view().substr(iid).replace("instanceIdentifier=\"", "")

	def get_target_file_region(self, sel):
		#get the line starting position
		line_start = self.window.active_view().line(sel).begin()

		#locate the executeFile call within that line
		return self.window.active_view().find("ExecuteFile\(\'[a-z0-9\_\.]+", line_start, sublime.IGNORECASE)

To get the double click working you will need to create a “Default (Window/OSX/Linux).sublime-mousemap” file as well. Just use the OS you are using in the parenthesis and paste the following code.


	{
		"button": "button1", "modifiers": "ctrl"], "count": 2,
		"command": "open_instance_vm_file"
	}
]

I forget if there is way to limit this mouse map to a specific file type. I know keyboard mapping can be done through context entries, but I couldn’t get it to work with this. Just use “CTRL+ DOuble Click” and that should launch the file.

I am sure this coudl be cleaned up quite a bit, but this should get you started.

0 Likes

#5

Absolutely stuning amazing great super… sorry for my poor english I dont know more superlatives 8)

only thing which was need, was changing your RegEx from [a-z0-9] to [a-zA-Z0-9] rest of your code works like a charm!

0 Likes

#6

Sorry, I forgot to include the sublime.IGNORECASE flag in the find_all call for instanceIdentifiers which should fix that as well if you didn’t want to add the capital letters to the regex. I am glad that worked for you!

0 Likes