1

When working on my TypeScript code there is an issue with the automatic indenting of code. I use VS2013 with Resharper.

The problem occurs mostly from method chaining.

For example using promises:

someService.GetSomeProperties().then(x => {
    return otherService.doSomethingWithX(x.map(y => y.id));
}).then(x => {
    // Pressing enter sets the cursor to this position
        // Using document format (CTRL+K,CTRL+D) the code moves to this level
        return '';
}).then(x => { // Pressing enter from here
    // Moves the previous line to it's position and cursor here
    return 'a';
    }).then(x => {
        // However document format moves this code block to this indention level
        // as well
    }).then(x => {
        // Follow up chaining remains the same
    });

Based on the 'symptoms' this behavior seems to be coming from having multi-line statements in general.

I've looked through both VS2013's options and R#s but I can't find any option that has any effect on this behavior.

Does anybody know how to fix it?

1

1 Answer 1

2

Does anybody know how to fix it?

It is fixed if you put then in a new line (i.e. each top level function gets its own line) and always use semicolons:

someService.GetSomeProperties()
    .then(x => {
        return otherService.doSomethingWithX(x.map(y => y.id));
    })
    .then(x => {
        // Pressing enter sets the cursor to this position
        // Using document format (CTRL+K,CTRL+D) the code moves to this level
        return '';
    })
    .then(x => { // Pressing enter from here
        // Moves the previous line to it's position and cursor here
        return 'a';
    })
    .then(x => {
        // However document format moves this code block to this indention level
        // as well
    })
    .then(x => {
        // Follow up chaining remains the same
    });
Sign up to request clarification or add additional context in comments.

3 Comments

How is this an answer to my question? Who recommends this style? AFAIK style is a matter of preference, I agree on the semicolons (and am wondering where you see any missing ones), but the then on a new line is is the same as people arguing whether you should write an else statement as } else { or with a newline } \n else {.
Semicolons are not just style, they can influence behavior, if they were solely used for aesthetic purposes I couldn't care less which one someone uses, as long as they're consistent about it. For example in Lua they're purely a matter of style, and can always be replaced with a space (except of course in string literals). .then on the other hand is unaffected whether you put it on the same line or the next one, so that's just style.
I agree with @Aidiakapi. Semicolons are widely considered "mandatory" even if in theory mostly optional. Putting then on a new line certainly is a matter of style, but if switching IDE is not an option, I'd prefer this over broken formatting – at least it's consistent and makes sense (unlike broken indentation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.