0

Is it possible to create a function to manipulate a text selection? I'd like to select a paragraph and tell Visual Studio Code (or in any other text editor) to execute a function on that selection.

I've tried to use code snippets, but it allows only simple manipulation.

I've also tried to use regular expressions, but I'd like to execute different code based on which text I'm selecting. (As far as I've seen, the interface allows only 1 regexp at a given time)

Text example:

1  Q4. Consider these classes, defined in separate source files, 
2  public class Test1{ 
3    public float aMethod(float a, float b) throws IOException {
4    }
5  }
6
7  public class Test2 extends Test1{
8  }
9
10 Which of the following methods would be legal at line 2 in class Test2?
11 A. float aMethod(float a, float b){}
12 B. public int aMethod(int a, int b) throws Exception{ }
13 C. public float aMethod(float a, float b) throws Exception{ }
14 D. public float aMethod(float p, float q){ }

I' like to select the wording (L1: Q4 (...) L10:Test2?) and press a shortcut to format as:

id: Q4
wording: |
  Consider these classes ... Test2?

Then, select L11A. (...) L14{}, press another shortcut, and format as:

alternatives:
  a: float aMethod(float a, float b){}
  b: public int aMethod(int a, int b) throws Exception{ }
  c: public float aMethod(float a, float b) throws Exception{ }
  d: public float aMethod(float p, float q){ }
Victor Ribeiro
  • 577
  • 7
  • 20
  • Visual Studio Code doesn't use normal regex, I think it's something like BRE or something. –  Jul 04 '18 at 22:08
  • Do you literally want the " … " or do you want all the text between "classes" and "Test2"? And is that a pipe character after "wording: "? And the line numbers don't actually occur in your file do they? Are they just there for explanation? – Mark Jul 05 '18 at 03:21
  • Are the newlines preserved in selection 1? In L1-L10? – Mark Jul 05 '18 at 03:38
  • I'd like to run different regular expressions on different areas of the 14 line text. For the first 8 lines, I'd run a regular expression with several match groups. For the 9-14 lines, I'd like to run another regular expression with other match groups. I'd like to highlight the text, press a shortcut and run the associated regular expression / substitution. – Victor Ribeiro Jul 05 '18 at 14:13
  • You can't do something like this in Sublime in a snippet (per your tag), but in Sublime you can create a custom `Command` that extracts the selection, does something with it, and replaces it with your results. Presumably the same is also possible in VSC. – OdatNurd Jul 05 '18 at 17:51
  • I am asking if the line numbers actually are part of your selection. Is the 1, 2, actually part of each selected line? – Mark Jul 08 '18 at 03:11
  • Sorry if I misunderstood. No, the line numbers were inserted here only for ease of reading... but are not part of the actual string. – Victor Ribeiro Jul 08 '18 at 23:05

0 Answers0