Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RStudio removing leading whitespace formatting from SQL

I realize that this is technically a formatting rather than programming question, but it's inter-related to an understanding of both SQL and RStudio, so I feel it's still appropriate.

I have a script written in Microsoft SQL Server Management Studio that is very intricately formatted for ease of viewing. For example:

SELECT (
        CASE WHEN Variable in (1,2)
             THEN 1
             ELSE 2
             END
       )

However, when I paste this into RStudio, it becomes something like:

SELECT (
    CASE WHEN Variable in (1,2)
    THEN 1
    ELSE 2
    END
)

It maintains a tab when preceded by an open parenthesis, but nothing else. I can manipulate the size of the tabs it does keep, but no matter what options I use in Tools>GlobalOptions>CodeEditing, I can't keep any other formatting. It doesn't look like a big deal from the above example, but my code consists of multiple nested queries and becomes very confusing without the formatting. Any advice is appreciated!

like image 306
Sean Branchaw Avatar asked Feb 11 '23 03:02

Sean Branchaw


1 Answers

I finally figured this out after asking the question, but I thought other people might have the same question, so I want to share the answer.

There's another option within Tools>GlobalOptions>CodeEditing that says "Auto-indent code after paste" that is checked as default. Apparently, it strips all your formatting AND adds its own tabs. Unchecking it fixed the problem.

like image 119
Sean Branchaw Avatar answered Mar 20 '23 04:03

Sean Branchaw