Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constructor + object initializer with opening brace on next line #154

Closed
asamwow opened this issue Feb 19, 2020 · 7 comments
Closed

constructor + object initializer with opening brace on next line #154

asamwow opened this issue Feb 19, 2020 · 7 comments
Labels

Comments

@asamwow
Copy link

asamwow commented Feb 19, 2020

for example this code

var response = new JsonResult(message)
{
    StatusCode = (int)HttpStatusCode.BadRequest
};

gets indented like this

var response = new JsonResult(message)
    {
        StatusCode = (int)HttpStatusCode.BadRequest
    };

because the opening brace line is considered a "statement-cont" c syntactic element, but should be a "block-open". I think the applicable code starts on line 794 at

(c-lang-defconst c-basic-matchers-after

I'm looking into it.

@josteink
Copy link
Collaborator

Great. If you have a PR I will be glad to review and merge it 😃

@asamwow
Copy link
Author

asamwow commented Feb 19, 2020

Hi, how does c-sharp mode set the c-syntactic-element if it does at all? I feel like one would need to add to c-special-indent-hook or override c-guess-basic-syntax? The only thing I can find that csharp-mode.el actually does is set font-lock-faces.

@josteink
Copy link
Collaborator

It may indeed not be immediately clear how csharp-mode operates.

To sum it up, csharp-mode inherits from cc-mode in base Emacs/ELPA, and then configures it for C# grammar. As such cc-mode does most of the heavy lifting, but it has a million parameters, and our main job/difficulty is configuring it properly.

When that gets hard or doesn't work, we try to provide our own overrides and implementations.

To adapt indentation we have advised c-inside-bracelist-p to redirect to csharp-inside-bracelist-p and c-forward-objc-directive to nil, but only for csharp-mode to avoid breaking other modes relying on cc-mode.

If you need to tweak indentataion-code, chances are you want to take a look at csharp-inside-bracelist-p.

If you need to add additional overrides, I advise you to use defadvice over straight out overrides. We've done that in the past, and it did increasingly create compatibility issues, until we removed all that code completely.

Good luck!

@asamwow
Copy link
Author

asamwow commented Feb 20, 2020

Hi, I didn't mean to imply csharp-mode was simple or bad in any way. I love the mode and use it every day! Unfortunately, I made 0 progress digging through the code. So since I have other responsibilities, here is my hack to rigidly decrease the indent in situations like the one on this issue. Still needs more testing, but seems to work for well except for nested constructors. Performance is terrible unsurprisingly.

(defun custom-csharp-indent-hook ()
  (if (eq major-mode 'csharp-mode)
      (save-excursion
        (previous-line)
        (let ((line (thing-at-point 'line)))
          (if (string-match "\\(^[^//\n]*new \\([^{\n]\\)*\\((\\w*)\\)?[^;\n,}{]$\\)\\|\\(=> *\\(new\\)?$\\)\\|\\(new$\\)" line)
              (progn
                (let ((isDotFunction (string-match "\\(^ +\\.\\|\\( *\\w* *(\\( *\\w *\\)*new\\)\\)\\|\\(=>$\\)" line)))
                  (setq indent -4)
                  (forward-line)
                  (let ((line2 (thing-at-point 'line)))
                    (if (string-match "^[^//\n]*{" line2)
                        (progn
                          (if isDotFunction
                              (setq indent -8))
                          (if (string-match "=> *new$" line)
                              (decrease-indent-at-point (+ indent 4))
                            (decrease-indent-at-point indent))))))))))))
(defun decrease-indent-at-point (indent)
  (message (number-to-string indent))
  (indent-rigidly(line-beginning-position) (line-end-position) indent))
(add-hook 'c-special-indent-hook #'custom-csharp-indent-hook)

@josteink
Copy link
Collaborator

No need to apologize. No offence taken.

C# is a expressive language with an complex grammar, and csharp-mode is kinda a beast trying to work with all that, effectively trying to represent a complete-enough C# parser on its own, written in one of the worst LISPs around.

There's guaranteed to be complexity issues with the code, no matter how you twist or turn it.

Not to mention, this project is really just some abandonware I picked up and decided to maintain after it completely broke with the Emacs 24.1 release or something.

I decided it was better with something non-perfect, but working for C# editing in Emacs, than nothing at all, and with that I became the maintainer of one of the most complex major-modes out there in the Emacs-verse. Yay me!

So far I've kept it mostly going, but I really don't have much time for this project anymore, and it shows with the outstanding issues.

If you sometime in the future have another issue and think you can help, feel free to help out. There's more Emacs-wielding C# users out there than you think. This mode is in the 90th percentile on MELPA after all! ;)

@theothornhill
Copy link
Collaborator

@asamwow
Hi!
Can you test the rework branch? This should be fixed there!

@josteink
Copy link
Collaborator

Yes this works too in the rework branch. This is just wonderful ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants