Home Download Buy Blog Forum Support

Help needed with a plugin in development

Help needed with a plugin in development

Postby maiasaura on Fri Mar 02, 2012 7:40 am

I'm new to python but making some progress. I'm developing a plugin to clean up code in R based on a style guide. I just need to make one replacement work and I can deal with the rest.

Here is one such clean up operation that I would need to do on a .r script. A standard function definition looks like this:

Code: Select all
function_name <- function(params) {
# stuff
}


The above is the right style. Sometimes code can look like one of the following:

Code: Select all
function_name <- function(params)
{
# stuff
}


or like this:
Code: Select all
function_name <- function(params)
            {
# stuff
}


etc.

I've written a regexp to capture anything between a ) and a {. I just need to reduce that to a single space between the two and add a newline after {

Below is a prototype of a package that I have going with filler comments where I can't figure stuff out.


Code: Select all
import sublime, sublime_plugin

class PrettyRCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      sel = self.view.sel()[0]

      poorly_formatted_functions = self.view.find('\\)\\s*(.*?)\\s\\{', sel.begin())
     #split poorly formatted code into two regions
     # add single space between region A and region B
     # insert new line after region B
     #insert corrected_code back into same spot

      self.view.insert(edit, sel.begin(), corrected_code)


Currently this would work with a keybinding that looks like this:
Code: Select all
{ "keys": ["ctrl+shift+k"], "command": "pretty_r" }


Issues: Not sure how to get the replacement in. Second, I don't just want to do this on just selected text, I want to do this for the entire document. Ideas?
maiasaura
 
Posts: 15
Joined: Mon Dec 19, 2011 2:31 am

Re: Help needed with a plugin in development

Postby agibsonsw on Fri Mar 02, 2012 8:06 pm

Try this:

Code: Select all
import sublime, sublime_plugin

class PrettyRCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      region = sublime.Region(0,self.view.size())
      results = self.view.find_all("\\)\s*\n\s*\\{")
      for r in results:
         self.view.replace(edit, r, ") {")
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Help needed with a plugin in development

Postby agibsonsw on Fri Mar 02, 2012 8:17 pm

This version will only run within files with extension '.r':

Code: Select all
import sublime, sublime_plugin

class PrettyRCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      if not self.view.file_name()[-2:] == '.r':
         print 'not in r'
         return
      region = sublime.Region(0,self.view.size())
      results = self.view.find_all("\\)\s*\n\s*\\{")
      for r in results:
         self.view.replace(edit, r, ") {")
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Help needed with a plugin in development

Postby Cjkjvfnby on Sat Mar 03, 2012 8:27 pm

maiasaura wrote:I've written a regexp to capture anything between a ) and a {. I just need to reduce that to a single space between the two and add a newline after {

Issues: Not sure how to get the replacement in. Second, I don't just want to do this on just selected text, I want to do this for the entire document. Ideas?


View
substr
size
find_all
replace
Cjkjvfnby
 
Posts: 20
Joined: Wed Feb 01, 2012 11:35 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 10 guests