4

I got plugin for sublime text 3 that let me move cursor to line by its number:

import sublime, sublime_plugin

class prompt_goto_lineCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.show_input_panel("Goto Line:", "", self.on_done, None, None)
        pass

    def on_done(self, text):
        try:
            line = int(text)
            if self.window.active_view():
                self.window.active_view().run_command("goto_line", {"line": line} )
        except ValueError:
            pass

class go_to_lineCommand(sublime_plugin.TextCommand):

    def run(self, edit, line):
        # Convert from 1 based to a 0 based line number
        line = int(line) - 1

        # Negative line numbers count from the end of the buffer
        if line < 0:
            lines, _ = self.view.rowcol(self.view.size())
            line = lines + line + 1

        pt = self.view.text_point(line, 0)

        self.view.sel().clear()
        self.view.sel().add(sublime.Region(pt))

        self.view.show(pt)

I want to improve it to let me move cursor to first line containing the specified string. It is like a search on file: For example if pass to it string "class go_to_lineCommand" plugin must move cursor to line 17 :

select line by text

and possibly select string class go_to_lineCommand.

The problem is reduced to finding regionWithGivenString, and then I can select it:

self.view.sel().add(regionWithGivenString)

But don't know method to get regionWithGivenString.

I tried to

  1. find on google: sublime plugin find and select text
  2. check api

But still no result.

Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166
  • why the hell you need plugin for that? Why you cannot just use Ctrl+G/Ctrl+F? – igor Nov 01 '13 at 06:53
  • it is just part of big plugin for fast navigating: imagine I got code in `iced coffee script`: `a = (require './c/d/e/file.js').doMethod()`. I move cursor on this line, press short key and this sublime plugin opens file `file.js` and select `doMethod` method for me. – Maxim Yefremov Nov 01 '13 at 10:39
  • 2
    I know you have a solution for this already, but just thought I would also point you in the right direction in terms of the docs. Take a look at `view#find` and `view#find_all`. They return a region and an array of regions respectively. Of course, the end result will be the same as the plugin provided by lhuang. – skuroda Nov 02 '13 at 06:11

3 Answers3

3

I am not sure about the typical way. However, you can achieve this in following way:

  1. Get the content of current doc.
  2. Search target string to find out its start and end position. Now you have the start and end point.
  3. Add the Region(start, end) to selections.

Example:

def run(self, edit, target):
    if not target or target == "":
        return

    content = self.view.substr(sublime.Region(0, self.view.size()))
    begin = content.find(target)
    if begin == -1:
        return
    end = begin + len(target)
    target_region = sublime.Region(begin, end)
    self.view.sel().clear()
    self.view.sel().add(target_region)
longhua
  • 4,142
  • 21
  • 28
2

there you have it in the API, use the view.find(regex,pos) method.

s = self.view.find("go_to_lineCommand", 0)
self.view.sel().add(s)

http://www.sublimetext.com/docs/3/api_reference.html

Ivan Ferrer Villa
  • 2,129
  • 1
  • 26
  • 23
1

A possible improvement to the longhua's answer - adding moving cursor to the target line.

class FindcustomCommand(sublime_plugin.TextCommand):
    def _select(self):
        self.view.sel().clear()
        self.view.sel().add(self._target_region)

    def run(self, edit):
        TARGET = 'http://nabiraem'

        # if not target or target == "":
        #   return

        content = self.view.substr(sublime.Region(0, self.view.size()))
        begin = content.find(TARGET)
        if begin == -1:
            return
        end = begin + len(TARGET)
        self._target_region = sublime.Region(begin, end)    
        self._select()
        self.view.show(self._target_region) # scroll to selection
sergzach
  • 6,578
  • 7
  • 46
  • 84