emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CC Mode lambda function help


From: Theodor Thornhill
Subject: Re: CC Mode lambda function help
Date: Fri, 09 Oct 2020 14:43:38 +0200

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]