all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* CC Mode lambda function help
@ 2020-09-25 21:05 Theodor Thornhill
  2020-10-09 10:57 ` Alan Mackenzie
  0 siblings, 1 reply; 3+ messages in thread
From: Theodor Thornhill @ 2020-09-25 21:05 UTC (permalink / raw)
  To: emacs-devel


Hello!

I am reworking the csharp mode found at
https://github.com/josteink/csharp-mode, and I have some problems I am
really struggling to fix, or find information about in the manual (if
it is there):

1. Lambda functions

Consider this code:

```
AsyncTest.Run(async () =>
{
    Something xyz =
        new SomethingMore();
});
```

Right now this indents as:
```
AsyncTest.Run(async () =>
              {
                  Something xyz =
                  new SomethingMore();
              });
```
I cannot find what c-lang-defconst to configure, or what c-style-alist
combination to pick to get this.  Any pointer to what is wrong would be
super helpful.

2. Attributes

Consider this code:

```
[DllImport("something.dll")]
public extern int GetSomething();

```

Gets indented as:
```
[DllImport("something.dll")]
    public extern int GetSomething();
```

This I attribute (pun intended) to vsemi, since when one is added:
```
[DllImport("something.dll")];  // <-- notice this
public extern int GetSomething();
```

It behaves correctly. In addition, that semi fixes fontification for the
method name. I would like not to use the 'statement-cont', since that
only looks like a symptom, rather than a fix.

I'd love some pointers to how I would best handle this. 

To reproduce this, you have to (unfortunately...) clone the
abovementioned repo, checkout the 'rework' branch, then byte compile
that file.  I am sorry there is no quicker way to get to that code.

These snippets should be enough to illustrate my issues.

Have a lovely day,
Theodor Thornhill




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

* Re: CC Mode lambda function help
  2020-09-25 21:05 CC Mode lambda function help Theodor Thornhill
@ 2020-10-09 10:57 ` Alan Mackenzie
  2020-10-09 12:43   ` Theodor Thornhill
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Mackenzie @ 2020-10-09 10:57 UTC (permalink / raw)
  To: Theodor Thornhill; +Cc: emacs-devel

Hello, Theodor.

I just noticed your post from a fortnight ago.  Sorry I didn't notice it
earlier.

On Fri, Sep 25, 2020 at 23:05:42 +0200, Theodor Thornhill wrote:

> Hello!

> I am reworking the csharp mode found at
> https://github.com/josteink/csharp-mode, and I have some problems I am
> really struggling to fix, or find information about in the manual (if
> it is there):

OK.  Version numbers?  ;-)

What does "the" manual mean?  If it doesn't mean the CC Mode manual,
then it should.  It's available in .../emacs/info/ccmode.info, or
some very similar address.  It contains a wealth of information, being
largely written by myself.  :-)

> 1. Lambda functions

> Consider this code:

> ```
> AsyncTest.Run(async () =>
> {
>     Something xyz =
>         new SomethingMore();
> });
> ```

> Right now this indents as:
> ```
> AsyncTest.Run(async () =>
>               {
>                   Something xyz =
>                   new SomethingMore();
>               });
> ```
> I cannot find what c-lang-defconst to configure, or what c-style-alist
> combination to pick to get this.  Any pointer to what is wrong would be
> super helpful.

OK, just to be quite clear, c-lang-defconsts are intended for the use of
hackers maintaining CC Mode or modes derived from it (as you are doing),
to specify modes.  They are not for users' configuration, for which see
things like "styles" and c-offsets-alist and other things in the
configuration section of the CC Mode manual.

I think it likely you have run into a configuration bug, possibly an old
bug, where a "syntactic offset" was inappropriately set.  To see the
syntactic symbol(s) for a source line, put point on it and do C-c C-s.
To see the offset currently configured for that/those symbol(s), do C-c
C-o (again, see the CC Mode manual for any needed explanations).

Running the CC Mode head on Emacs master, I don't see the problems
you're having.  But I haven't yet downloaded the branch from github that
you mention.

> 2. Attributes

> Consider this code:

> ```
> [DllImport("something.dll")]
> public extern int GetSomething();

> ```

> Gets indented as:
> ```
> [DllImport("something.dll")]
>     public extern int GetSomething();
> ```

> This I attribute (pun intended) to vsemi, since when one is added:
> ```
> [DllImport("something.dll")];  // <-- notice this
> public extern int GetSomething();
> ```

> It behaves correctly. In addition, that semi fixes fontification for the
> method name. I would like not to use the 'statement-cont', since that
> only looks like a symptom, rather than a fix.

> I'd love some pointers to how I would best handle this. 

I think you know the answer: fix csharp--at-vsemi-p.  It seems intended
to detect the attribute in square brackets and signal a virtual
semicolon.  Why is it not working?

> To reproduce this, you have to (unfortunately...) clone the
> abovementioned repo, checkout the 'rework' branch, then byte compile
> that file.  I am sorry there is no quicker way to get to that code.

> These snippets should be enough to illustrate my issues.

I hope this answer from me is at least somewhat helpful.

> Have a lovely day,

Yourself, too!

> Theodor Thornhill

-- 
Alan Mackenzie (Nuremberg, Germany).



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

* Re: CC Mode lambda function help
  2020-10-09 10:57 ` Alan Mackenzie
@ 2020-10-09 12:43   ` Theodor Thornhill
  0 siblings, 0 replies; 3+ messages in thread
From: Theodor Thornhill @ 2020-10-09 12:43 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: emacs-devel


Hello!

>
> What does "the" manual mean?  If it doesn't mean the CC Mode manual,
> then it should.  It's available in .../emacs/info/ccmode.info, or
> some very similar address.  It contains a wealth of information, being
> largely written by myself.  :-)

I'm using CC Mode manual from the master branch, recompiled
regularly-ish.  Also the code has progressed quite a bit since this mail
was sent, so the issues have largely been solved (though I'm still
unsure whether the solutions I picked are considered bad/good style) 

[...]

>
> OK, just to be quite clear, c-lang-defconsts are intended for the use of
> hackers maintaining CC Mode or modes derived from it (as you are doing),
> to specify modes.  They are not for users' configuration, for which see
> things like "styles" and c-offsets-alist and other things in the
> configuration section of the CC Mode manual.
>

Yeah! Right now the rework branch is merged into master, and as such it
should be easy to get the code I'm describing.  

> I think it likely you have run into a configuration bug, possibly an old
> bug, where a "syntactic offset" was inappropriately set.  To see the
> syntactic symbol(s) for a source line, put point on it and do C-c C-s.
> To see the offset currently configured for that/those symbol(s), do C-c
> C-o (again, see the CC Mode manual for any needed explanations).

Yeah, these bindings are essential, and I use them all the time :-)


Actually, I fixed this behaviour by adding a new syntax construct by
advising the appropriate functions.  Exactly what I have done to get the
lambdas working correctly is:

--------------------------------------------------
(advice-add 'c-looking-at-inexpr-block
            :around 'csharp-looking-at-inexpr-block)

(defun csharp-looking-at-inexpr-block (orig-fun &rest args)
  (let ((res (csharp-at-lambda-header)))
    (if res
        res
      (apply orig-fun args))))

(defun csharp-at-lambda-header ()
  (unless (bobp)
    (save-excursion
      (c-backward-syntactic-ws)
      (backward-char)
      (c-safe (goto-char (scan-sexps (point) -1)))
      (when (or (looking-at "([[:alnum:][:space:]_,]*)[ \t\n]*=>[ \t\n]*{")
	        (looking-at "[[:alnum:]_]+[ \t\n]*=>[ \t\n]*{"))
        ;; If we are at a C# lambda header
        (cons 'inexpr (point))))))
--------------------------------------------------

As for the Attributes part:
[...]

>
> I think you know the answer: fix csharp--at-vsemi-p.  It seems intended
> to detect the attribute in square brackets and signal a virtual
> semicolon.  Why is it not working?

I got this working using vsemi some time ago, but I've moved to using
the 'annotation-top-cont' as used in 'java-mode', since those are
similar enough.

The way I did this is:

--------------------------------------------------
(advice-add 'c-guess-basic-syntax
            :around 'csharp-guess-basic-syntax)

(defun csharp-guess-basic-syntax (orig-fun &rest args)
  (cond
   (;; Attributes
    (save-excursion
      (goto-char (c-point 'iopl))
      (and
       (eq (char-after) ?\[)
       (save-excursion
         (c-go-list-forward)
         (and (eq (char-before) ?\])
              (not (eq (char-after) ?\;))))))
    `((annotation-top-cont ,(c-point 'iopl))))
   (t
    (apply orig-fun args))))
--------------------------------------------------
Again, this enables 'csharp-mode' to detect and use the available
syntactic constructs, and would also enable people to configure this in
their 'styles-alists'

I am not sure if this is at all 'ok' seen from your perspective, but
this was the only way I could get these constructs to indent correctly
without either monkey-patching or creating some very complicated things
myself.  I try to keep things very simple, relying only on CC Mode apis,
but some things just doesn't seem to work out of the box. 

However, there is one bug still with this that not the mentioned
approach nor vsemi helped with. Fontification for the line after the
attribute/annotation doesn't work. That is, keywords are still
fontified, but types are not.  I think this has to do with the
fontification levels, and the complex-font-lock mechanisms.  This is
described in fairly good detail in:

https://github.com/josteink/csharp-mode/issues/142 

I think I need to use the 'c-type' 'c-decl-end' text properties, but I
cannot seem to understand how that works right now.  I've tried to look
at how Objective-C handles the '@'-annotations, but to no avail yet :)


Apart from these things, I think I have most everything covered, apart
from twiddling the fontification, and the multiline strings issues I
seem to have. (Mentioned in a separate issue - I'll respond to your
thorough answer there).  The code base is reduced from 3500 lines to
less than 600, while keeping all the same functionality. 

>
>> To reproduce this, you have to (unfortunately...) clone the
>> abovementioned repo, checkout the 'rework' branch, then byte compile
>> that file.  I am sorry there is no quicker way to get to that code.
>
>> These snippets should be enough to illustrate my issues.

No need to do this anymore. It is available from Melpa now.

--
Theo



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

end of thread, other threads:[~2020-10-09 12:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-25 21:05 CC Mode lambda function help Theodor Thornhill
2020-10-09 10:57 ` Alan Mackenzie
2020-10-09 12:43   ` Theodor Thornhill

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.