Skip to content

Define a variable for locally customizing indentation #320

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

Merged
merged 3 commits into from
Sep 28, 2015
Merged

Define a variable for locally customizing indentation #320

merged 3 commits into from
Sep 28, 2015

Conversation

Malabarba
Copy link
Member

No description provided.

clojure-backtracking-indent is still optional (the option still works),
but instead of using its own symbol property, it uses
clojure-indent-function instead.

The clojure-backtracking-indent symbol property actually still works,
but is obsolete.
@Malabarba
Copy link
Member Author

The first commit merged the two symbol properties into one.
I did it because it will make things simpler on the CIDER side, and I think I did it in a backwards compatible way. Let me know if you disagree.

This variable provides a way of overriding the clojure-indent-function
symbol property on a buffer-local basis.
@Malabarba
Copy link
Member Author

Ok, I see now that merging the two like this is not backwards compatible.
Could you explain to me how the backtracking spec works? I'll need to understand it if I'm to merge the two properly.

@bbatsov
Copy link
Member

bbatsov commented Sep 11, 2015

Could you explain to me how the backtracking spec works? I'll need to understand it if I'm to merge the two properly.

Wish I could. :-) This is where we see the failure of lack of documentation and tests.

@bbatsov
Copy link
Member

bbatsov commented Sep 11, 2015

Btw, I wrote a bit more about this on gitter. This whole functionality has a funny story - it was introduced in the very first version of clojure-mode in 2008 and has been changed little since. The only big change is that at some point someone asked for it to be enabled by default. The only documentation is the code (which at some point I was superficially familiar with) - no docs, no tests, no nothing. I remember I figured out the workings at some point mostly by looking at the default config (alas this knowledge has faded by now and I don't have time to rehash it). Frankly, I'd rather dispose of it and have just one indentation function which understands more complex indentation specifications by default.

You have carte blanche to deal with this as you see fit, as long we simplify the inner workings in the process.

@Malabarba
Copy link
Member Author

Ok, I may have abused your carte blanche here.
Rewrote the backtracking code from scratch, and changed quite a bit of the regular code as well.
It also relies a bit less on lisp-mode, which fixed another couple of bugs.
The new form of indent specs is described in the commit message. I'll document it, of course, but I want to hear the opinions of other people first, including the clojure-list.

@bbatsov
Copy link
Member

bbatsov commented Sep 13, 2015

The new form of indent specs is described in the commit message. I'll document it, of course, but I want to hear the opinions of other people first, including the clojure-list.

Haven't went through the changes in great detail, but they look good to me. You'll definitely won't get much feedback about the quality of new code on the Clojure mailing list, but might receive some comments about the structure of the indentation spec at least.

@Malabarba
Copy link
Member Author

but might receive some comments about the structure of the indentation spec at least

Oh yes, that's what I meant.

@Malabarba
Copy link
Member Author

Fixed the docstring and added some tests for normal-indent commit.

The other commit didn't actually change any functionality. So the existing tests should be a great way to verify it's validity. :-)

@bbatsov
Copy link
Member

bbatsov commented Sep 14, 2015

The other commit didn't actually change any functionality. So the existing tests should be a great way to verify it's validity. :-)

There were zero tests for the backtracking indent... :-) Adding tests in an unavoidable task at this point.

@Malabarba
Copy link
Member Author

Ok. I've added a couple of tests and I think I'm ready to merge this.

[Fix #282 Fix #296] Use custom-code to calculate normal-indent.

Add tests for backtracking indent and several other scenarios.

Support a new notation for indent specs.
An indent spec can be:
- `nil` (or absent), meaning “indent like a regular function”.
- A list/vector meaning that this function/macro takes a number of
  “special” arguments which are indented by more spaces (in CIDER
  that's 4 spaces), and then all other arguments are indented like a
  body (in CIDER that's 2 spaces).
  - The first element is a number indicating how many "special"
    arguments this function/macro takes.
  - Each following element is an indent spec on its own, and it
    applies to the argument on the same position as this element. So,
    when the argument is a form, it specifies how to indent that
    argument internally (if it's not a form the spec is irrelevant).
  - If the function/macro has more aguments than the list has
    elements, the last element of the list applies to all remaining
    arguments.

So, for instance, if I specify the `deftype` spec as `[2 nil nil fn]`
(equivalent to `[2 nil nil [1]]`), it would be indented like this:

```
(deftype
    ImageSelection
    [data]
  Transferable
  (getTransferDataFlavors [this]
    (some-function))
  SomethingElse
  (isDataFlavorSupported [this flavor]
    (= imageFlavor flavor)))
```
(I put `ImageSelection` and `[data]` on their own lines just to show the indentation).

Another example, `reify` as `[1 nil fn]`
```
(reify Object
  (toString [this]
    (f)
    asodkaodkap))
```

Or something more complicated, `letfn` as `[1 [fn] nil]`
```
(letfn [(twice [x]
          (* x 2))
        (six-times [y]
          (* (twice y) 3))]
  (println "Twice 15 =" (twice 15))
  (println "Six times 15 =" (six-times 15)))
```
bbatsov added a commit that referenced this pull request Sep 28, 2015
Define a variable for locally customizing indentation
@bbatsov bbatsov merged commit 27c2a6f into clojure-emacs:master Sep 28, 2015
@bbatsov
Copy link
Member

bbatsov commented Sep 28, 2015

On a related note - the section in the README about indentation has to be updated/extended.

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

Successfully merging this pull request may close these issues.

2 participants