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

Indentation bug (parentheses) #74

Open
pyloolex opened this issue Jan 10, 2023 · 4 comments
Open

Indentation bug (parentheses) #74

pyloolex opened this issue Jan 10, 2023 · 4 comments

Comments

@pyloolex
Copy link

If round parentheses are on different lines, the indentation gets broken.

Try to write this code in the kotlin-mode:

        val binding: FragmentSleepQualityBinding = DataBindingUtil.inflate(
            inflater, R.layout.fragment_sleep_quality, container, false)

And press Enter.

Expected result:
The cursor is here

        val binding: FragmentSleepQualityBinding = DataBindingUtil.inflate(
            inflater, R.layout.fragment_sleep_quality, container, false)
        | <- cursor

Actual result:
The cursor is here

        val binding: FragmentSleepQualityBinding = DataBindingUtil.inflate(
            inflater, R.layout.fragment_sleep_quality, container, false)
            | <- cursor
@taku0
Copy link
Contributor

taku0 commented Jan 10, 2023

I would write kotlin-mode--prev-line-same-level something like this:

(defun kotlin-mode--prev-line-same-level ()
  "Move to nearest preceeding non-empty line with same nesting level.

If an unmatched open bracket is found, move to the position after it.

If an unmatched close bracket is found, move to the beginning of the buffer."
  (condition-case nil
      (while (not (kotlin-mode--bol-other-than-spaces-p))
        (backward-sexp))
    (scan-error
     (forward-comment (- (point)))
     (when (memq (char-before) '(?} ?\) ?\]))
       (goto-char (point-min))))))

(defun kotlin-mode--bol-other-than-spaces-p ()
  "Return non-nil if the point is at the beginning of line other than spaces.

Return nil otherwise"
  ;; omitted
  )

Then, replace (while not-indented ...) loop with something like this:

(forward-comment (- (point)))
(if (and (eq (char-before) ?>)
         (eq (char-before (1- (point))) ?-))
    ;; previous line ends with ->
    (setq cur-indent (+ (current-indentation) kotlin-tab-width))
  (kotlin-mode--prev-line-same-level)
  (setq cur-indent (+ (current-indentation)
                      (if (memq (char-before) '(?{ ?\( ?\[))
                          kotlin-tab-width
                        0))))

Those code are not tested.

@taku0
Copy link
Contributor

taku0 commented Jan 10, 2023

It would still mishandle those cases:

Expected:

foo(1,
    2,
    3)

Actual:

foo(1,
  2,
  3)

Expected:

let x = foo +
  bar

Actual:

let x = foo +
bar

My branch works fine for those cases: #53.

@taku0
Copy link
Contributor

taku0 commented Jan 10, 2023

Arrow (->) is not handled properly.

Expected:

when (x) {
    aaa() ->
        // This line should be aligned with `aaa` with offset.
        bbb()
    ccc() ->
        ccc()
}

foofoofoo { x ->
    // This line should be aligned with `foofoofoo` with offset, not `x`.
    bar(x)
}

Actual:

when (x) {
    aaa() ->
        // This line should be aligned with `aaa` with offset.
        bbb()
    ccc() ->
        ccc()
}

foofoofoo { x ->
                // This line should be aligned with `foofoofoo` with offset, not `x`.
                bar(x)
}

After all, indenting Kotlin code is not a hundred lines of simple thing, but thousands of lines of complex logic.

More pathological example:

when (foo) {
    object: A1,
            A2 {
    },
    object: A3 by object: A4,
                          A5 {
            },
            A6 {
    } ->
        1
}

@taku0
Copy link
Contributor

taku0 commented Mar 21, 2023

Now fixed as #53 is merged.

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

No branches or pull requests

2 participants