简体   繁体   English

IDE和编译器的/ t间距是否不同? (IntelliJ上的JAVA)

[英]Do IDEs and compilers have different spacing for /t spacing? (JAVA on IntelliJ)

I am pretty new to programming and just started my computer science degree. 我是编程新手,刚开始攻读计算机科学学位。 In class I was given an assignment to create a pattern. 在课堂上,我被分配了创造模式的作业。 They use jGRASP as their IDE in class while I use IntelliJ. 他们在课堂上使用jGRASP作为他们的IDE,而我使用IntelliJ。

I created a pattern using the IntelliJ IDE to display "AEV" as displayed in the screen shot Code and output ScreenShot . 我使用IntelliJ IDE创建了一个模式,以显示“ AEV”,如屏幕快照Code和输出ScreenShot所示 I used blank spaces and /t to create the pattern. 我使用空格和/ t创建模式。 To check if my code looks the same as they want I took the .java file and ran it in another IDE called jGRASP. 为了检查我的代码是否与他们想要的代码相同,我获取了.java文件并在另一个名为jGRASP的IDE中运行了该文件。 Now on jGRASP the spacing is all off jGRASP code and output ScreenShot . 现在,在jGRASP上,间距全部由jGRASP代码和输出ScreenShot决定 I couldn't figure out what caused it. 我不知道是什么原因造成的。

Do "/t" have different spacing for different IDEs? 不同的IDE的“ / t”间距是否不同? Or is there some setting that I need to change? 还是我需要更改某些设置?

Very likely. 很有可能。 The standard tab width is 8, but some people prefer to display 4-column tabs, or even 2-column tabs. 标准制表符宽度为8,但有些人更喜欢显示4列制表符,甚至显示2列制表符。 There's also people who prefer to indent with tabs, and people that prefer to indent with spaces. 还有一些人倾向于使用制表符缩进,而某些人则倾向于使用空格进行缩进。 This is the stuff flame wars are fought over. 这是战斗所需要的东西。 Whichever camp you're in, the worst possible choice is to mix the two in the same file. 无论您处于哪个营地,最糟糕的选择就是将两者混合在同一文件中。 As long as all indents are tabs, it doesn't matter how tabs are displayed, it will always work okay. 只要所有缩进都是制表符,制表符的显示方式都无所谓,它总是可以的。 If there are no tabs and all indents are spaces, it matters even less what the tab width is. 如果没有制表符并且所有缩进都是空格,那么制表符宽度的大小就无关紧要。 But if you make a hybrid file, expect trouble. 但是,如果您制作混合文件,那就麻烦了。

Similarly for the output: in general, I'd only use Tab semantically, like in TSV to separate records, never for precise formatting, precisely because you have no control over how it will be viewed. 对于输出类似:通常,我只会在语义上使用Tab,就像在TSV中那样来分隔记录,绝不会进行精确的格式设置,正是因为您无法控制如何查看它。

Do IDEs and compilers have different spacing for /t spacing? IDE和编译器的/ t间距是否不同? (JAVA on IntelliJ) (IntelliJ上的JAVA)

The compiler doesn't care. 编译器不在乎。

IDE's typically typically have a default TAB width that you can change. 通常,IDE通常具有可以更改的默认TAB宽度。 The default may depend on the host operating system. 默认值可能取决于主机操作系统。

The real problem is that operating systems have different defaults ... that you typically can't change. 真正的问题是操作系统具有不同的默认值……您通常无法更改。

Problems arise if you write your source code on (say) Windows and your co-worker uses Linux or Mac. 如果您在(例如)Windows上编写源代码并且您的同事使用Linux或Mac,则会出现问题。 If you use TAB and you don't agree to configure your IDEs to have the same number of spaces for TAB, your code will look ugly to your co-worker, and vice versa. 如果您使用TAB,但您不同意将IDE配置为具有相同数量的TAB空格,那么您的代码对您的同事将很难看,反之亦然。

The best solution is to not use the TAB character for indentation. 最好的解决方案是不要使用TAB字符进行缩进。 Configure your IDE to use spaces for indentation. 配置您的IDE以使用空格进行缩进。 Then your code will be correctly indented irrespective of the IDE or OS anyone uses to view it. 然后, 无论任何人用来查看它的IDE或OS,您的代码都将正确缩进。 Note that modern IDEs can easily be configured to translate a TAB keystroke into the appropriate number of spaces for an indentation level. 请注意,可以将现代IDE轻松配置为将TAB击键转换为缩进级别的适当数量的空格。


However I think my question was a bit unclear. 但是我认为我的问题还不清楚。 I was wondering why the "\\t" in a print() would have different output on run I/O window. 我想知道为什么print()中的“ \\ t”在运行I / O窗口上会有不同的输出。

That is entirely down to how the console displays the TAB character in the output stream. 这完全取决于控制台如何在输出流中显示TAB字符。 The rendering of TAB to some number of fixed width spaces is typically done by some OS specific / OS provided software. 将TAB渲染到一定数量的固定宽度空间通常是由某些特定于OS或OS提供的软件完成的。 It is outside of Java's control. 它超出了Java的控制范围。 Indeed, the JVM typically find out how (or even if) this rendering will occur, so it cannot reasonably intervene. 实际上,JVM通常会发现这种渲染将如何发生 (或什至),因此无法合理地干预。

When console output is displayed by an IDE, the IDE takes care of the rendering. 当IDE显示控制台输出时,IDE将负责呈现。

Would you know why the spacing in the output is different for IntelliJ vs jGRASP? 您知道为什么IntelliJ和jGRASP的输出间距不同吗?

That would be a design decision by the people who implemented the respective tools. 那将是实施相应工具的人员的设计决定。 But I imagine you can configure those tools to behave differently to their respective default settings. 但是我想您可以配置这些工具使其行为与各自的默认设置不同。 Check the configuration settings ... 检查配置设置...

But the bottom line is that if you want console text spacing to be consistent across all real / virtual consoles, output SP characters rather than TABs. 但最重要的是,如果您希望控制台文本间距在所有真实/虚拟控制台之间保持一致,请输出SP字符而不是TAB。

Finally found a solution. 终于找到了解决方案。 The problem was indeed with the tab spacing for the two IDEs. 实际上,问题出在两个IDE的制表符间距上。 jGrasp by default uses 3 spacing tabs while IntelliJ for Java uses 4 spacing. 默认情况下,jGrasp使用3个空格选项卡,而IntelliJ for Java使用4个空格。

On jGRASP if you click on settings > Font > CSD > Tab size : uncheck button to edit. 在jGRASP上,如果单击设置>字体> CSD>制表符大小:取消选中按钮进行编辑。 jGrasp setup jGrasp设置

On IntelliJ it is a bit harder to change the tab since it creates a project when you first create it. 在IntelliJ上,更改选项卡有点困难,因为它是在您首次创建项目时创建的。

Go to preferences > Editor > CodeStyle : uncheck editor config support. 转到首选项>编辑器> CodeStyle:取消选中编辑器配置支持。 If this is kept checked then the editorconfig file in the package overrides the IDE settings. 如果保持选中状态,则程序包中的editorconfig文件将覆盖IDE设置。 IntelliJ Code style setup IntelliJ代码样式设置

CodeStyle dropdown> select file (Java) > Tabs and Indents > set Tab to 3 IntelliJ Java File setup Click apply. CodeStyle下拉菜单>选择文件(Java)>制表符和缩进>将制表符设置为3 IntelliJ Java文件设置单击应用。

Finally go to CodeStyle again and click export on editorconfig to replace the file on the project. 最后,再次转到CodeStyle,然后在editorconfig上单击“导出”以替换项目中的文件。

The EditorConfig file and the editorconfig support option is what causes Intellij to be more confusing. EditorConfig文件和editorconfig支持选项使Intellij更加混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM