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

Formatter indents on ternary operator #18

Closed
ewerybody opened this issue Dec 20, 2020 · 8 comments
Closed

Formatter indents on ternary operator #18

ewerybody opened this issue Dec 20, 2020 · 8 comments
Assignees
Labels
bug something isn't working formatter code formatter issues

Comments

@ewerybody
Copy link

Repro

  1. have sth like this code (this is from the original docs just with quotes around the 3)
var := x>y ? 2 : "3"
something("skdfjbv")
  1. Perform Format Document with AHK++ >
var := x>y ? 2 : "3"
    something("skdfjbv")

The exact expression from the docs var := x>y ? 2 : 3 does work properly tho!
It's just when there is a quoted string after the :

Expected behavior
The code should not change.

Environment

VS Code 1.53.0.0
AutoHotkey Plus Plus v2.5.12

@ewerybody ewerybody added the bug something isn't working label Dec 20, 2020
@ewerybody
Copy link
Author

I would actually like to have a look into this myself but I have NO idea where to start as I'm not into TS or Electron at all. But that can change :)

I guess everything is already build-in to VS Code? To debug TS? Where would I start setting breakpoints? Can I debug the running VS Code while coding it ... ?!?! 🤔

@FuPeiJiang
Copy link

to help understand: since strings are removed, it becomes var := x>y ? 2 :(var purityText in \vscode-autohotkey-plus-plus\src\provider\formattingProvider.ts), and anything that ends with : is considered a hotkey, so it is indented

my git is kinda messed up right now... I'm learning how to use, now I have time
I will try to fix this after

@ewerybody
Copy link
Author

hmm. But

var := x>y ? 2 : 3
something("skdfjbv")

remains unchainged!

After all: I think we need some tests for the formatter at best on various bigger libraries and.

@FuPeiJiang
Copy link

3 is not a string
I said strings are removed
3 here is a number

@fade2gray
Copy link

fade2gray commented Dec 23, 2020

@ewerybody
Maybe this might help understand. Try formatting this.

;Formatting ignores any string after the colon
;making the ternary operator act like a hotkey.
var := x>y ? 1 : 2 ;<< Integer after tthe : is ok
;identation no
var := x>y ? 1 : ;<< empty value after : causing indentation
;everything indented
;until something
;ends with a : or  : "string" or Return
var := x>y ? 1 : "2" ;<< "string" ignored causing indentation
;everything indented
;until something
;ends with a : or  : "string" or Return
HotKey:
;everything indented
;until something
;ends with a : or  : "string" or Return
Return

@ewerybody
Copy link
Author

Ah ok. I didn't get what you mean with "strings removed". Thought you mean quotes removed or something.
"string" ignored is a little more clear. Thanks @fade2gray
But what about the ; why aren't these ignored? See:

;Formatting ignores any string after the colon
;making the ternary operator act like a hotkey.
var := x>y ? 1 : 2 ;<< Integer after tthe : is ok
;identation no
var := x>y ? 1 : ;
;identation no
var := x>y ? 1 : "2" ;
;identation no
HotKey:;
;identation no
Return

@fade2gray
Copy link

fade2gray commented Dec 23, 2020

Because the ternary operator is only affected if it finishes with a string or nothing at all.

var := x>y ? 1 : "2" ; Here, the string is ignored but formatting still sees the semicolon so it is not interpreted as hotkey/label.

@FuPeiJiang
Copy link

"string" ignored is a little more clear.

noted

But what about the ; why aren't these ignored? See:

ty for finding this, this isn't supposed to happen, since one ; is a comment too
the reason for this is:
.replace(/;.+/, '') + is one or more
if you use this instead, the empty comment will be ignored, (I like to say removed because it's being replaced with empty string)
.replace(/;.*/, '') * is zero or more

this is in function CodeUtil.purity of \vscode-autohotkey-plus-plus\src\common\codeUtil.ts

but I suspect that changing ONLY this regex won't fix the problem of matching comments properly...
since `; is not a comment

I will try to fix these, after...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something isn't working formatter code formatter issues
Projects
None yet
Development

No branches or pull requests

4 participants