简体   繁体   English

为什么我的源代码(用Eclipse编写)在其他文本编辑器中看起来不同?

[英]Why does my source code (written in Eclipse) look different in other text editors?

I've been using Eclipse to do my CS assignments, as recommended by my professor. 按照教授的建议,我一直在使用Eclipse进行CS分配。 However, I've noticed that if I open my source code in a different text editor, my beautiful, perfect formatting looks wrong. 但是,我注意到,如果我在其他文本编辑器中打开源代码,那么我漂亮,完美的格式看起来是错误的。 I believe the problem lies in tabs. 我相信问题出在标签上。 A tab character seems to take up less space in eclipse than in other text editors. 在Eclipse中,制表符似乎比其他文本编辑器占用更少的空间。

A good chunk of our grade is determined by the neatness of our code. 我们成绩的很大一部分取决于我们代码的整洁程度。 I'm not sure if our programs are graded in eclipse or not, so I'd like to figure out how to make source code have the same formatting regardless of text editor. 我不确定我们的程序是否在Eclipse中分级,所以我想弄清楚无论文本编辑器如何使源代码具有相同的格式。

Is this a problem with Eclipse? Eclipse是否有问题? Are there settings I can fiddle with? 有我可以摆弄的设置吗?

This is probably due to your settings for the tab symbol . 这可能是由于您对选项卡符号进行的设置 If you really want to indent using tab, make sure it is set to 8 spaces everywhere. 如果您真的想缩进使用制表符,请确保将其设置为8个空格。

From the Java coding convention : 根据Java编码约定

Four spaces should be used as the unit of indentation. 四个空格应用作缩进单位。 The exact construction of the indentation (spaces vs. tabs) is unspecified. 缩进的确切结构(空格与制表符)未指定。 Tabs must be set exactly every 8 spaces (not 4) . 制表符必须完全每8个空格(而不是4个)设置

Personally I always use spaces to indent my code due to the fact that some people have their tab symbol set to show as 4 spaces. 就个人而言,由于某些人的制表符设置为4个空格,因此我总是使用空格来缩进代码。

To set Eclipse to always use spaces, go to 要将Eclipse设置为始终使用空格,请转至

Window -> Preferences -> Java -> Code style -> Formatter -> Edit 窗口 -> 首选项 -> Java- > 代码样式 -> 格式化程序 -> 编辑

and set Tab policy to Spaces Only . 并将“ Tab”策略设置为“ 仅空格”

The tab size is probably different in other editors. 选项卡的大小在其他编辑器中可能有所不同。 It still is in its raw form a \\t . 它仍然是原始形式\\t Depending on what the editor will display when it encounters one is probably found in the preference. 在首选项中可能会找到编辑器,具体取决于编辑器在遇到它时将显示的内容。 Either way it should be consistent in size all the way across the file. 无论哪种方式,它在整个文件中的大小都应保持一致。

You could also convert tabs to spaces so it is always the same regardless of editor. 您还可以将制表符转换为空格,因此无论使用什么编辑器,它始终是相同的。

The tab character does not have a defined display width. 制表符没有定义的显示宽度。 In notepad, it is displayed as the same width as 8 spaces wide. 在记事本中,它显示为与8个空格相同的宽度。 All code editors should allow the viewer to change the displayed tab width. 所有代码编辑器都应允许查看者更改显示的标签宽度。 The convention for code is normally a width equivalent to 4 spaces. 代码约定通常的宽度等于4个空格。

If you're desperate, you could replace all tabs with 4 spaces. 如果您不顾一切,可以将所有制表符替换为4个空格。 However, this is frowned upon by some, and may lose you marks. 但是,某些人对此并不满意,并且可能会给您留下痕迹。 I'm pretty sure that the advised java coding style advocates the use of tabs, not spaces. 我非常确定,建议的Java编码风格主张使用制表符,而不是空格。

At the end of the day, the marker is a fool if he'll turn down syntax highlighting, and the code editor that comes with it. 归根结底,如果标记器拒绝语法高亮显示以及随之而来的代码编辑器,那么它就是一个傻瓜。

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

相关问题 为什么我的listPreference对话框看起来不同? - Why does my listPreference dialog look different? 当我使用编辑器代码生成器时,为什么eclipse会在构造函数中自动添加java super()方法? - Why does eclipse automatically add a java super() method in a constructor when I use the editors code generator? 为什么我的JButton在不同的计算机上看起来有所不同? - Why does my JButton look differently of different computers? 为什么我的JavaFX窗口到处看起来都不一样? - Why does my JavaFX window look different everywhere? 为什么我的代码在 readLine() 处卡在 Eclipse 中? - Why does my code stuck in eclipse at readLine()? 为什么我的RichJLabel文本的一部分看起来被覆盖/隐藏了? - Why does part of my RichJLabel text look covered/hidden? 为什么此Java代码在一个Eclipse项目中而不在另一个Eclipse项目中生成错误? - Why does this Java code generate an error in one Eclipse project but not the other? 为什么热点在同一源代码中使用不同的程序集 styles? - Why does hotspot use different assembly styles in same source code? Eclipse:将源代码的源文件夹导入到其他项目中 - Eclipse: import folder of source code to other projects 如何识别项目(eclipse)类型(RCP或其他)来查看代码? - How to identify a project (eclipse) type (RCP or other) to look at the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM