all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Regex matching summary blocks in C#
@ 2023-12-03 12:07 Samvid Mistry
  2023-12-03 13:39 ` Yuri Khan
  0 siblings, 1 reply; 6+ messages in thread
From: Samvid Mistry @ 2023-12-03 12:07 UTC (permalink / raw)
  To: help-gnu-emacs

Hey,

I am trying to write an overlay that will simplify the large `<summary>`
blocks around comments in C# and just keep the content. For example,

```
/// <summary>
/// Gets the value of the counter.
/// </summary>
```

will get simplified to just

```
// Gets the value of the counter.
```

I have written a regex that can match the summary blocks, that is

"\\(\\s-*\\)///\\s-*<summary>\\s-*\n\\(.*\n\\)*?\\s-*///\\s-*</summary>"

The first group captures the indentation so that the overlay is indented
properly. The second group is supposed to capture the text of the block.
It works fine for single line blocks but for block spanning multiple
lines, the second capture group only captures the last line of comment.
The remaining lines get captured by `///\\s-*<summary>\\s-*\n` somehow.

For example, in

```
    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="Service"/>
    /// class.
    /// </summary>
```

the second capture group only captures ` /// class.` and all lines above
it are captured by the former part of the regex. Not sure what I am
doing wrong here. I haven't put any wildcards that will capture anything
other than whitespace around summary tag. Appreciate any help with
this.

PS: This is my first time using any mailing list. Let me know if I
should be following any conventions while posting.

-- 
Thanks,
Samvid



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Regex matching summary blocks in C#
  2023-12-03 12:07 Regex matching summary blocks in C# Samvid Mistry
@ 2023-12-03 13:39 ` Yuri Khan
  2023-12-03 14:19   ` Samvid Mistry
  0 siblings, 1 reply; 6+ messages in thread
From: Yuri Khan @ 2023-12-03 13:39 UTC (permalink / raw)
  To: Samvid Mistry; +Cc: help-gnu-emacs

On Sun, 3 Dec 2023 at 19:20, Samvid Mistry <mistrysamvid@gmail.com> wrote:

> I have written a regex that can match the summary blocks, that is
>
> "\\(\\s-*\\)///\\s-*<summary>\\s-*\n\\(.*\n\\)*?\\s-*///\\s-*</summary>"
>
> The first group captures the indentation so that the overlay is indented
> properly. The second group is supposed to capture the text of the block.
> It works fine for single line blocks but for block spanning multiple
> lines, the second capture group only captures the last line of comment.

You have a *? iterator around your second group, so only one line is
captured. I’d try making the sub-regex that matches a single line into
a non-capturing group, then putting a capturing group around the
iterator:

    \\(\\(?:.*\n\\)*?\\)

(BTW what is your plan after you get the whole inside of the <summary>
in a single group? Your description makes it look like you’re going to
want to do something to display /// as //.)



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Regex matching summary blocks in C#
  2023-12-03 13:39 ` Yuri Khan
@ 2023-12-03 14:19   ` Samvid Mistry
  2023-12-03 14:30     ` Yuri Khan
  0 siblings, 1 reply; 6+ messages in thread
From: Samvid Mistry @ 2023-12-03 14:19 UTC (permalink / raw)
  To: Yuri Khan; +Cc: help-gnu-emacs


Yuri Khan <yuri.v.khan@gmail.com> writes:

>
> You have a *? iterator around your second group, so only one line is
> captured. I’d try making the sub-regex that matches a single line into
> a non-capturing group, then putting a capturing group around the
> iterator:
>
>     \\(\\(?:.*\n\\)*?\\)

IIUC, you are suggesting I do this

"\\(\\s-*\\)///\\s-*<summary>\\s-*\n\\(\\(:?.*\n\\)*?\\)\\s-*///\\s-*</summary>"

That doesn't seem to change anything.

>
> (BTW what is your plan after you get the whole inside of the <summary>
> in a single group? Your description makes it look like you’re going to
> want to do something to display /// as //.)

I'm removing the <(/)summary> tags from view. There are other ceremonies
like documentation for parameters and return values, which I'm
also planning to simplify. It is partly for aesthetics and partly for me
to try my hand at overlays.

-- 
Thanks,
Samvid



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Regex matching summary blocks in C#
  2023-12-03 14:19   ` Samvid Mistry
@ 2023-12-03 14:30     ` Yuri Khan
  2023-12-03 14:56       ` Samvid Mistry
  0 siblings, 1 reply; 6+ messages in thread
From: Yuri Khan @ 2023-12-03 14:30 UTC (permalink / raw)
  To: Samvid Mistry; +Cc: help-gnu-emacs

On Sun, 3 Dec 2023 at 21:23, Samvid Mistry <mistrysamvid@gmail.com> wrote:

> >     \\(\\(?:.*\n\\)*?\\)
>
> IIUC, you are suggesting I do this
>
> "\\(\\s-*\\)///\\s-*<summary>\\s-*\n\\(\\(:?.*\n\\)*?\\)\\s-*///\\s-*</summary>"

\\(?:, not \\(:? . The former marks the group as non-capturing. The
latter introduces a capturing group starting with a colon.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Regex matching summary blocks in C#
  2023-12-03 14:30     ` Yuri Khan
@ 2023-12-03 14:56       ` Samvid Mistry
  2023-12-03 15:15         ` tomas
  0 siblings, 1 reply; 6+ messages in thread
From: Samvid Mistry @ 2023-12-03 14:56 UTC (permalink / raw)
  To: Yuri Khan; +Cc: help-gnu-emacs


Yuri Khan <yuri.v.khan@gmail.com> writes:

> On Sun, 3 Dec 2023 at 21:23, Samvid Mistry <mistrysamvid@gmail.com> wrote:
>
>> >     \\(\\(?:.*\n\\)*?\\)
>>
>> IIUC, you are suggesting I do this
>>
>> "\\(\\s-*\\)///\\s-*<summary>\\s-*\n\\(\\(:?.*\n\\)*?\\)\\s-*///\\s-*</summary>"
>
> \\(?:, not \\(:? . The former marks the group as non-capturing. The
> latter introduces a capturing group starting with a colon.

That was my bad. Your expression seems to have worked. Thanks!

Would you mind expaining a bit about why it works? Per my current
understanding, both mine and yours should have the same net effect,
except that marking the inner group non-capturing should make the regex
evaluation a bit faster because emacs need not remember the matched
text. But clearly, my understanding isn't correct.

-- 
Thanks,
Samvid



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Regex matching summary blocks in C#
  2023-12-03 14:56       ` Samvid Mistry
@ 2023-12-03 15:15         ` tomas
  0 siblings, 0 replies; 6+ messages in thread
From: tomas @ 2023-12-03 15:15 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1095 bytes --]

On Sun, Dec 03, 2023 at 08:26:51PM +0530, Samvid Mistry wrote:
> 
> Yuri Khan <yuri.v.khan@gmail.com> writes:
> 
> > On Sun, 3 Dec 2023 at 21:23, Samvid Mistry <mistrysamvid@gmail.com> wrote:
> >
> >> >     \\(\\(?:.*\n\\)*?\\)
> >>
> >> IIUC, you are suggesting I do this
> >>
> >> "\\(\\s-*\\)///\\s-*<summary>\\s-*\n\\(\\(:?.*\n\\)*?\\)\\s-*///\\s-*</summary>"
> >
> > \\(?:, not \\(:? . The former marks the group as non-capturing. The
> > latter introduces a capturing group starting with a colon.
> 
> That was my bad. Your expression seems to have worked. Thanks!
> 
> Would you mind expaining a bit about why it works? Per my current
> understanding, both mine and yours should have the same net effect,
> except that marking the inner group non-capturing should make the regex
> evaluation a bit faster because emacs need not remember the matched
> text. But clearly, my understanding isn't correct.

Not Yuri here, but I think your expression does match several times,
and it keeps the last match. Yuri's captures the whole bunch just
once.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-12-03 15:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-03 12:07 Regex matching summary blocks in C# Samvid Mistry
2023-12-03 13:39 ` Yuri Khan
2023-12-03 14:19   ` Samvid Mistry
2023-12-03 14:30     ` Yuri Khan
2023-12-03 14:56       ` Samvid Mistry
2023-12-03 15:15         ` tomas

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.