Home Download Buy Blog Forum Support

Fold Away Lines Not Matching A Pattern

Fold Away Lines Not Matching A Pattern

Postby dalek67 on Wed Jul 04, 2012 2:27 pm

Back in my IBM 3270 mainframe days we used an editor called XEdit. It had a neat feature whereby you could hide away all lines in a file not matching a pattern or keyword.

There is something similar for Vim, called foldsearch (http://www.vim.org/scripts/script.php?script_id=2302).

I suspect some smart person could write a Sublime plugin to do this. Maybe one exists and I haven't found it yet.
dalek67
 
Posts: 3
Joined: Wed Jul 04, 2012 2:22 pm

Re: Fold Away Lines Not Matching A Pattern

Postby quarnster on Wed Jul 04, 2012 2:44 pm

I did this for my ADB logcat plugin. It shouldn't be too hard for someone to adapt it to a separate plugin. Cutting and pasting the relevant code bit:

Code: Select all
            view.run_command("unfold_all")
            endline, endcol = view.rowcol(view.size())
            line = 0
            currRegion = None
            regions = []
            while line < endline:
                region = view.full_line(view.text_point(line, 0))
                data = view.substr(region)
                if self.filter.search(data) == None:
                    if currRegion == None:
                        currRegion = region
                    else:
                        currRegion = currRegion.cover(region)
                else:
                    if currRegion:
                        regions.append(currRegion)
                        currRegion = None
                line += 1
            if currRegion:
                regions.append(currRegion)
            view.fold(regions)
quarnster
 
Posts: 389
Joined: Tue Nov 29, 2011 11:34 am

Re: Fold Away Lines Not Matching A Pattern

Postby dalek67 on Wed Jul 04, 2012 10:21 pm

quarnster wrote:I did this for my ADB logcat plugin. It shouldn't be too hard for someone to adapt it to a separate plugin. Cutting and pasting the relevant code bit:

Code: Select all
            view.run_command("unfold_all")
            endline, endcol = view.rowcol(view.size())
            line = 0
            currRegion = None
            regions = []
            while line < endline:
                region = view.full_line(view.text_point(line, 0))
                data = view.substr(region)
                if self.filter.search(data) == None:
                    if currRegion == None:
                        currRegion = region
                    else:
                        currRegion = currRegion.cover(region)
                else:
                    if currRegion:
                        regions.append(currRegion)
                        currRegion = None
                line += 1
            if currRegion:
                regions.append(currRegion)
            view.fold(regions)


Outstanding. I cobbled this together despite my horrible Python skills. I called it All because that's what it was called back on the mainframe.

Code: Select all
import sublime, sublime_plugin

class AllCommand(sublime_plugin.TextCommand):

def done(self,arg):
   self.foldstr = arg
   self.all()

def change(self,arg):
   pass
   
def cancel(self):
   pass

def all(self):
    view = self.view
    view.run_command("unfold_all")
    endline, endcol = view.rowcol(view.size())
    line = 0
    currRegion = None
    regions = []

    while line < endline:
       region = view.full_line(view.text_point(line, 0))
       data = view.substr(region)
       if data.find(self.foldstr) == -1:
            if currRegion == None:
               currRegion = region
            else:
               currRegion = currRegion.cover(region)
       else:
            if currRegion:
               regions.append(currRegion)
               currRegion = None
       line += 1
       if currRegion:
         regions.append(currRegion)
       view.fold(regions)

def run(self, edit):
    window = self.view.window()
    window.show_input_panel("Fold Away Except","",self.done,self.change,self.cancel)


Thanks for your help.
dalek67
 
Posts: 3
Joined: Wed Jul 04, 2012 2:22 pm

Re: Fold Away Lines Not Matching A Pattern

Postby dalek67 on Fri Jul 06, 2012 12:30 pm

If anyone is playing along I made this work MUCH faster.

Code: Select all
import sublime, sublime_plugin

class AllCommand(sublime_plugin.TextCommand):

def done(self,arg):
   self.foldstr = arg
   self.all()

def all(self):
    view = self.view
    view.run_command("unfold_all")
    endline, endcol = view.rowcol(view.size())
    line = 0
    firstRegion = None
    currRegion = None
    regions = []

    while line < endline:
       region = view.full_line(view.text_point(line, 0))
       data = view.substr(region)
       if data.find(self.foldstr) == -1:
            if firstRegion == None:
               firstRegion = region
               lastRegion  = region
            else:
               lastRegion  = region
       else:
            if firstRegion:
               currRegion = firstRegion.cover(lastRegion)
               regions.append(currRegion)
               firstRegion = None
       line += 1
       if currRegion:
         regions.append(currRegion)
         currRegion = None

    if firstRegion:
      currRegion = firstRegion.cover(lastRegion)
      regions.append(currRegion)
      firstRegion = None         
    view.fold(regions)

def run(self, edit):
    window = self.view.window()
    for reg in self.view.sel():
      defstr = self.view.substr(reg)
      break
    window.show_input_panel("Show Only Lines Containing",defstr,self.done,None,None)
dalek67
 
Posts: 3
Joined: Wed Jul 04, 2012 2:22 pm

Re: Fold Away Lines Not Matching A Pattern

Postby kdoto on Wed Aug 29, 2012 8:30 am

Maybe someone can explain to me how to use this plugin?

I have placed the plugin into a file called "all.py" in the Packages/User directory.

Then I call view.run_command("all") from the console, and nothing happens.

So how does one make use of this plugin?

Code: Select all
import sublime, sublime_plugin

class AllCommand(sublime_plugin.TextCommand):

    def done(self,arg):
       self.foldstr = arg
       self.all()

    def all(self):
        view = self.view
        view.run_command("unfold_all")
        endline, endcol = view.rowcol(view.size())
        line = 0
        firstRegion = None
        currRegion = None
        regions = []

        while line < endline:
           region = view.full_line(view.text_point(line, 0))
           data = view.substr(region)
           if data.find(self.foldstr) == -1:
                if firstRegion == None:
                    firstRegion = region
                    lastRegion  = region
                else:
                    lastRegion  = region
           else:
                if firstRegion:
                    currRegion = firstRegion.cover(lastRegion)
                    regions.append(currRegion)
                    firstRegion = None
           line += 1
           if currRegion:
              regions.append(currRegion)
              currRegion = None

        if firstRegion:
            currRegion = firstRegion.cover(lastRegion)
            regions.append(currRegion)
            firstRegion = None
        view.fold(regions)

def run(self, edit):
    window = self.view.window()
    for reg in self.view.sel():
        defstr = self.view.substr(reg)
        break
    window.show_input_panel("Show Only Lines Containing",defstr,self.done,None,None)
kdoto
 
Posts: 3
Joined: Wed Aug 29, 2012 8:28 am

Re: Fold Away Lines Not Matching A Pattern

Postby vitaLee on Wed Aug 29, 2012 9:07 am

you should trigger this command in context of a view.
either add keybinding in your User/Default (OS).sublime-keymap
Code: Select all
{ "keys": ["ctrl+alt+["], "command": "all" }


to trigger thourgh command menu add this to User/Default.sublime-commands
Code: Select all
{
  "caption": "Fold All Not Matching",
  "command": "all"
}
vitaLee
 
Posts: 150
Joined: Tue Dec 27, 2011 11:10 pm

Re: Fold Away Lines Not Matching A Pattern

Postby kdoto on Wed Aug 29, 2012 9:48 am

vitaLee wrote:you should trigger this command in context of a view.
either add keybinding in your User/Default (OS).sublime-keymap
Code: Select all
{ "keys": ["ctrl+alt+["], "command": "all" }


I tried this and ctrl+alt+" does nothing.

Doesn't this do the exact same thing as doing
Code: Select all
view.run_command("all")
from the console anyway though?
kdoto
 
Posts: 3
Joined: Wed Aug 29, 2012 8:28 am

Re: Fold Away Lines Not Matching A Pattern

Postby vitaLee on Wed Aug 29, 2012 10:43 am

you're right, that should do the job too.
any errors for you in the console or in dialogs ?
do you have all.pyc file next to all.py in Packages/User ?
vitaLee
 
Posts: 150
Joined: Tue Dec 27, 2011 11:10 pm

Re: Fold Away Lines Not Matching A Pattern

Postby kdoto on Wed Aug 29, 2012 11:16 am

vitaLee wrote:any errors for you in the console or in dialogs ?

No :(

vitaLee wrote:do you have all.pyc file next to all.py in Packages/User ?

Yes.
kdoto
 
Posts: 3
Joined: Wed Aug 29, 2012 8:28 am

Re: Fold Away Lines Not Matching A Pattern

Postby sylvanaar on Wed Sep 12, 2012 10:32 am

Thanks this works great. This was the one thing that I really needed this editor to do. Great for working with log files.

Only had to fix the indenting. Thanks so much!!

Edit: Here is my contribution. the "all_toggle" command. You can bind it, and once you perform your all command you can easily toggle back and forth between showing all lines and showing the result of all

Code: Select all
import sublime, sublime_plugin

class AllCommand(sublime_plugin.TextCommand):

   def done(self,arg):
      self.foldstr = arg      
      self.all()

   def all(self):
       view = self.view
       view.run_command("unfold_all")
       endline, endcol = view.rowcol(view.size())
       line = 0
       firstRegion = None
       currRegion = None
       regions = []

       while line < endline:
          region = view.full_line(view.text_point(line, 0))
          data = view.substr(region)
          if data.find(self.foldstr) == -1:
               if firstRegion == None:
                  firstRegion = region
                  lastRegion  = region
               else:
                  lastRegion  = region
          else:
               if firstRegion:
                  currRegion = firstRegion.cover(lastRegion)
                  regions.append(currRegion)
                  firstRegion = None
          line += 1
          if currRegion:
            regions.append(currRegion)
            currRegion = None

       if firstRegion:
         currRegion = firstRegion.cover(lastRegion)
         regions.append(currRegion)
         firstRegion = None         
       view.fold(regions)
       view.settings().set("all", self.foldstr)

   def run(self, edit):
       if self.view.settings().has("all"):
          self.done(self.view.settings().get("all"))
          return
       window = self.view.window()
       for reg in self.view.sel():
         defstr = self.view.substr(reg)
         break
       window.show_input_panel("Show Only Lines Containing",defstr,self.done,None,None)


class AllToggleCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      view = self.view
      if view.settings().has("all"):
         if len(view.folded_regions()):
            view.run_command("unfold_all")
         else:
            view.run_command("all")   
sylvanaar
 
Posts: 1
Joined: Wed Sep 12, 2012 10:11 am

Next

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests