Sublime Forum

Select current block of text [Solved]

#1

Hi,

I want to edit a plugin that parses a (code) block of text.
A block is comparable to a paragraph, thus all text that is inbetween newlines, or from linenr 1 until the first newline or end of file.

Is there a Sublime method available for that?

Currently the user has to manually select the code block, which is not ideal.

0 Likes

#2

In the selection menu:

Selection -> Expand to paragraph

0 Likes

#3

How can I get the Paragraph selection in a plugin ?

Currently the pugin uses this method to retrieve the current selection:

def getSelection():
    text = []
    if View().sel():
        for region in View().sel():
            if region.empty():
                text.append(View().substr(View().line(region)))
            else:
                text.append(View().substr(region))
    return text

How could I write a method to get the current paragraph where the cursor is ?

Ideally I’d like to:

  • Check if there is currently a selection.
  • If there is something selected, use that
  • If there is no selection, get the paragraph where the cursor is
0 Likes

#4

Probably the easiest thing to do is:

  1. If there is no selection, expand the selection to the current paragraph

  2. Do what you want with the current selection.

This gives your command the robustness to allow the user to select exactly what they want and provide a sensible default if they don’t.

As @math2001 mentioned, you can use the built in functionality to expand the selection to the current paragraph; if there is no selection, this will select the paragraph that the cursor is currently in.

The command to do this is expand_selection_to_paragraph (which you can determing by entering sublime.log_commands (True) in the sublime console and then selecting the menu item to see what it’s doing). From the looks of the code above you already have a handle on how to determine if the current selection is empty.

In order to run the command, you can do something like:

view.run_command ("expand_selection_to_paragraph")
0 Likes

#5

Actually you can just import that function at the top of the file via from Default.paragraph import expand_to_paragraph.
Then use it text.append(View().substr(expand_to_paragraph(View(), region.b))).

2 Likes

#6

I couldn’t get it to work on ST2… Is there a way? (i’m on st3)

0 Likes

#7

Can you post your code? In this simple test, it seems to work OK for me (cursor was in the middle of the first line when I ran the command).

0 Likes

#8

Ohh, sorry. I meant the import from a plugin inside a package:

# ${packages}/User/test.py
from __future__ import absolute_import

from Default import expand_to_paragraph # ImportError: No module named Default.paragraph

from ..default.paragraph import expand_to_paragraph # ValueError: Attempted relative import in non-package
0 Likes

#9

Ahh yeah, I see it works OK in ST3 but not in ST2. I’m not sure if that’s because ST2 is using python 2.6 or if ST2 doesn’t expose other packages the same way as ST3 does (I’m not overly familiar with either, unfortunately).

According to the plugin porting guide:

Importing other plugins is simpler and more robust in Sublime Text 3, and can be done with a regular import statement, e.g., import Default.comment will import Packages/Default/Comment.py.

This makes it seem like it’s still possible via some other mechanism in ST2, but I was unable to find any example of how that would be done.

The workaround of using view.run_command() to execute the command directly works in both versions. If you’re targeting both versions of sublime you could perhaps use it as a fallback for ST2.

0 Likes

#10

Thanks!!

This works in S3:

from Default.paragraph import expand_to_paragraph

def getSelection():
    text = []
    if View().sel():
        for region in View().sel():
            if region.empty():
                text.append(View().substr(expand_to_paragraph(View(), region.b)))
            else:
                text.append(View().substr(region))
    return text

Applied in https://github.com/mtxr/SQLTools/pull/55/commits/dc256452da5d7e1744fe92fe014a30d97860728f

Marked topic as [Solved]

Thanks @OdatNurd for the ‘sublime.log_commands (True)’ tip! & thanks @r-stein for the solution :slightly_smiling:
& thanks @math2001 for the ST2 support :slightly_smiling:

0 Likes

#11

Oh, I know! I need to edit sys.path

# -*- encoding: utf-8 -*-

import sys
import sublime
import os

sys.path.append(os.path.join(sublime.packages_path(), 'Default'))
from Default.paragraph import expand_to_paragraph
sys.path.pop() # removes the last one

This way is working. :slightly_smiling:

1 Like

#12

Have a look at the plugin troubleshooting. There’s a command that allows you to toggle different loggers.

0 Likes