Hi there,
My first post on the forum, so I hope the formatting is acceptable!
I have a curious (to me) issue with a Plug In that I’m toying with. I’ve had success with Plug In development previously, but prior functionality has always been related to working on the self object…
This time, my intention is to create a command that will edit a large number of differing source types.
The command will read from a text file which lists the path of each applicable source file ( I’ll create this filelist in advance ), and then process each individual source file in turn using regex etc to identify the required snippets of code for amendment. ( I can’t just use regex replacements, as it needs to be more context-sensitive than that (and readable!) ).
In the stripped-back code below, you can see that I create the class StepThroughFilesCommand(sublime_plugin.TextCommand) which then opens the filelist and loops through it, calling ProcessSingleFile(self, view) for each entry in the filelist. I invoke the command from a key-mapped macro.
Within ProcessSingleFile() there is a sublime.message_dialog(displaytext)
With the message_dialog in place, the contents of each source file are captured as expected. Great!
BUT
if I comment out the message_dialog (as would be required for bulk processing of files), it doesn’t work!
The message_dialog call seems to make the new ProcessSingleFile() view active/focussed, and without this call the contents end up showing the details of the buffer/view that I had open when I invoked the macro.
Can anyone tell me what/why this happens? Am I missing something obvious?
Thanks in advance!
Darren
##################################################
import sublime
import sublime_plugin
import re
import time
##################################
# StepThroughFilesCommand
##################################
class StepThroughFilesCommand(sublime_plugin.TextCommand):
def run(self, edit, **keywordedargs):
userpath = 'C:\\Users\\someone\\AppData\\Roaming\\Sublime Text 3\\Packages\\User\\'
target_filelist = userpath + keywordedargs['filelist']
sublime.message_dialog( 'StepThroughFiles target_filelist:' + target_filelist )
with open( target_filelist ) as filelist:
for singlefile in filelist.read().splitlines():
sublime.message_dialog( 'singlefile : ' + singlefile )
# open the file in a new tab, and pass it as a View
self.ProcessSingleFile(sublime.active_window().open_file(singlefile))
sublime.active_window().run_command("close_file")
sublime.message_dialog('StepThroughFiles end')
#----------------------------------------------------------------------------------
def ProcessSingleFile(self, view):
displaytext = 'PBIF() view .id() :' + str( view.id() ) + ' .buffer_id() :' + str( view.buffer_id() ) + ' .file_name : ' + view.file_name() + ' .size : ' + str( view.size() ) # size is 0!
# if this message_dialog is not executed before reference to size(), contents are empty and it doesnt work!!?
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# sublime.message_dialog(displaytext)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
print(displaytext)
contents = view.substr(sublime.Region(0, view.size()))
sublime.message_dialog('after opening.... contents :' + contents )
# The intention is to step through the view for context-sensitive adjustments to source files :
#
# r_source = sublime.Region(0, view.size())
# r_source_split = view.split_by_newlines(r_source)
#
# for vr_source_line in r_source_split:
# vs_source_string = view.substr(vr_source_line)
# vs_source_string_len = len(vs_source_string)
# sublime.message_dialog('Source: '+str(vs_source_string)+' source_line_len:'+str(vs_source_string_len))
#if (sublime.active_window().active_view().change_count() > 0):
# sublime.active_window().run_command("save")