Author Topic: begin/end for quotes  (Read 953 times)

garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
begin/end for quotes
« on: October 10, 2022, 10:40:51 PM »
Hi all. Is there a way to use the "find_matching_paren", but for quotes? I'm currently using go, and I cant modify the begin/end pairs (v27.0.0.5). I would use this for selecting/copying/deleting.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6867
  • Hero Points: 528
Re: begin/end for quotes
« Reply #1 on: October 11, 2022, 02:48:07 AM »
There isn't anything built-in yet. Seems like a select-quoted-string command could be useful for doing what you want.

garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
Re: begin/end for quotes
« Reply #2 on: October 11, 2022, 04:33:35 PM »
If you're gonna add this as a feature request (yes please!), I would love it to be a part of the find_matching_paren... I've been hitting that keybinding trying to do this for a few years now, as it feels 'natural' that it should be the same keystroke...

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6867
  • Hero Points: 528
Re: begin/end for quotes
« Reply #3 on: October 11, 2022, 06:53:00 PM »
This is much harder to do than I thought. Many languages have some sort of string interpolation. Some even support an expression which can contain a string expression inside the string. Finding the start and end can't be done accurately for every language/every string sample. Strings in some languages (often called here documents), don't even start with a typical string character of " ' or `.

SlickEdit does know if a double quote character is in string color but any assumption made about the end quote can be confused by string interpolation. In some cases, you can't tell if you're at the start or end of the string due to interpolation. It is possible to get it right most of the time. You can certainly give up for the case where you can't tell if you're at the start or end of a string. I can't think of a good way to find the start/end of here document strings either.

It's definitely possible to write something which works for most strings which start with "  ' or `.

Dennis

  • Senior Community Member
  • Posts: 3961
  • Hero Points: 517
Re: begin/end for quotes
« Reply #4 on: October 11, 2022, 07:02:39 PM »
You could try this command.  I will add this code to the next release (at this point, 27.0.1).

Code: [Select]
/**
 * Selects the text from the start to end of the current token under the cursor.
 * If there is no token under the cursor, selects the whitespace between the
 * previous token and the next token after the cursor.
 *
 * @see select_word
 * @see select_subword
 * @see tag_get_current_token
 * @see tag_get_next_token
 * @see tag_get_prev_token
 *
 * @appliesTo Edit_Window, Editor_Control
 * @categories Edit_Window_Methods, Editor_Control_Methods, Selection_Functions
 */
_command void select_token() name_info(','VSARG2_MULTI_CURSOR|VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   if (!_is_tokenlist_supported()) {
      _message_box("Token list not supported for this language");
      return;
   }

   if (_lineflags() & NOSAVE_LF) {
      select_line();
      return;
   }

   _UpdateContextAndTokens(AlwaysUpdate:true, ForceUpdate:false);

   start_offset := 0;
   end_offset   := p_buf_size;
   offset := _QROffset();
   tid := tag_get_current_token((int)offset);
   if (tid > 0) {
      tag_get_token_info(tid, auto token_type, auto token_text, auto token_seek, auto token_line);
      start_offset = token_seek;
      end_offset   = token_seek + length(token_text);
      //SAY("HAVE TOKEN: start="start_offset" end="end_offset);
   } else {
      while (--offset > 0) {
         tid = tag_get_current_token((int)offset);
         if (tid > 0) break;
      }
      if (tid > 0) {
         tag_get_token_info(tid, auto token_type, auto token_text, auto token_seek, auto token_line);
         start_offset = token_seek + length(token_text);
         //SAY("HAVE PREV TOKEN: start="start_offset);
         tid = tag_get_next_token(tid);
      } else {
         tid = tag_get_first_token();
      }
      if (tid > 0) {
         //SAY("HAVE NEXT TOKEN: end="end_offset);
         tag_get_token_info(tid, auto token_type, auto token_text, auto token_seek, auto token_line);
         end_offset = token_seek;
      }
   }

   mark := _duplicate_selection("");
   _deselect(mark);
   _GoToROffset(start_offset);
   _select_char(mark);
   _GoToROffset(end_offset);
   _select_char(mark,translate(def_select_style,'N','I'));
   _cua_select=1;
}

garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
Re: begin/end for quotes
« Reply #5 on: October 13, 2022, 05:39:03 PM »
I tried to load that, but failed... (SlickC noob here)... I created a file with that code, and then attempted to Load Module via the menus... It then gave me errors (why cant I copy/paste from that "message list' window?).. Stuff about VSARG2_MULTI_CURSOR, VSARG2_READ_ONLY, VSARG2_REQUIRES_EDITORCTL not being initialized.. I'm guessing I'm missing an include or something?

If it makes any difference, OSX, intel, Brief, 27.0.0.5.
« Last Edit: October 13, 2022, 05:48:44 PM by garion911 »

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6867
  • Hero Points: 528
Re: begin/end for quotes
« Reply #6 on: October 13, 2022, 06:20:21 PM »
Try loading this attached macro file


garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
Re: begin/end for quotes
« Reply #7 on: October 13, 2022, 06:57:20 PM »
Yep, that does work. I tried it out in go and python, and it works there. In shell and yaml, it gives the error that the languages dont support token-list.

Now that I'm thinking about it... I wonder if a "find next matching character" would be closer/easier (maybe already exists?).. So if I put my cursor on the beginning quote, I could hit the keybinding, and it would go to the next quote.. If thats not the quote I want, I just hit it again. It also does not need to be selected, just moving the cursor is fine, unless in select mode of course. You could limit it to certain (configurable) characters..

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6867
  • Hero Points: 528
Re: begin/end for quotes
« Reply #8 on: October 16, 2022, 02:11:06 PM »
Now that Dennis came up with a very good way to select a string token, we plan (27.0.1) to enhance find_matching_paren to toggle between the beginning and end of a string if the cursor is sitting on a " ' or ` in string color.

Attached is an improved version of select_token.e which falls back to using color coding if token list information is not available (shell script languages and some others). When color coding is used, it simply selects all text under the cursor in the same color.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: begin/end for quotes
« Reply #9 on: October 16, 2022, 05:53:44 PM »
Enhancing find_matching_paren this way is a very good idea :)

garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
Re: begin/end for quotes
« Reply #10 on: October 18, 2022, 07:50:07 PM »
Awesome. Looking forward to that next version!