Giter Club home page Giter Club logo

Comments (7)

josteink avatar josteink commented on July 17, 2024

Thanks for the bug report. Double-width characters are known to cause problems in naive code...

I'll see if I can't investigate this later on.

from typescript.el.

josteink avatar josteink commented on July 17, 2024

I've tested with the following simple code:

class Testy {
    test() {
        let x = "大塚 愛";
    }
}

Upon pressing ; at the end there, I do get weird behaviour.

For a scenario similar to yours I get more behaviour like this:

class Testy {
    test() {
        test("大塚 愛", function()
    }
}

Upon typing the opening paren in function(), the cursor gets moved thee characters ahead of where it should be.

Basically: After three wide characters the cursor is now three points ahead of where it should actually be! I refuse to believe that is a coincidence.

What seems to trigger is this section here: https://github.com/ananthakumaran/typescript.el/blob/master/typescript-mode.el#L484-L485

(and (not (nth 8 syntax))
      typescript-auto-indent-flag)

I'll be honest and say I have no idea what that's supposed to mean, but when this code reports t back, what gets run is indent-according-to-mode, and that's what's triggering this behaviour. Could this be an error in indent.el?

Running indent-according-to-mode manually on any portion of the code with these wide characters does seem to lead to weird things, with the cursor-position often being calculated incorrectly.

For a quick workaround, I suggest you just set the typescript-auto-indent-flag defcustom to nil. This will suppresses this behaviour (and conceals the bug).

In the meantime, I'm not sure if this is a bug in our mode or if there are unicode errors in core Emacs functions. I'll try to get some help in these matters.

from typescript.el.

josteink avatar josteink commented on July 17, 2024

I've found the culprit. It's our code.

(defun typescript-indent-line ()
  "Indent the current line as typescript."
  (interactive)
  (save-restriction
    (widen)
    (let* ((parse-status
            (save-excursion (syntax-ppss (point-at-bol))))
           (offset (- (current-column) (current-indentation))))
      (indent-line-to (typescript--proper-indentation parse-status))
(when (> offset 0) (forward-char offset)))))

We're doing some weird calculations here, inside typescript--proper-indentation, and that results in a "offset" between the original and the indented version, and a attempt to fix this mismatch.

Clearly we're calculating some things incorrectly here, and that's causing issues further up the stack.

Edit: This analysis is wrong. See comments further down.

from typescript.el.

Kethku avatar Kethku commented on July 17, 2024

I just started running into this when I switched to using fira code with the symbols font for pseudo ligatures. Any idea if this will get fixed?

from typescript.el.

josteink avatar josteink commented on July 17, 2024

Update: I hate to leave code to rot, so I decided to a minimal effort debugging.

I used the following test-buffer:

export class Moo {
    let x = "abc";
    let y = "大塚愛";
}

Pressing ; after the line with "abc" resulted in typescript--proper-indentation returning 4 (which is correct).

Pressing ; after the line with "大塚愛" also resulted in typescript--proper-indentation returning 4 (which again is the correct value).

So clearly the error is in the offset-calculation in typescript-indent-line where we try to regain our original position after re-indenting the line. That should be much more manageable to analyse and debug, even for non-Elisp experts.

from typescript.el.

josteink avatar josteink commented on July 17, 2024

Nailed it: The problem is current-column counts wide-chars as several characters, causing a API-wise impedance-mismatch with forward-char (which counts all chars as 1 char).

This is easy to demonstrate. Take the following line in any mode:

abcd大塚愛

Put the cursor after the d, and eval (current-column). It should print 4.

Now put the cursor after the , and eval (current-column). Our algorithm expects it to return 5, but it returns 6.

Are there any options to current-column which correctly accounts for wide characters?

from typescript.el.

josteink avatar josteink commented on July 17, 2024

I've added a new function typescript--current-column which is unicode-aware and employed that instead in typescript-indent-line.

As far as I can see, this fixes this issue.

I also observe that we use current-column lots of other places in our code, but until someone can demonstrate that this is causing issues elsewhere, in the name of cautiousness I've left those as is.

If we have issues they should then be trivial to migrate to typescript--current-column.

from typescript.el.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.