코드 형식을 지정하는 아이디어 방법: 1. 단축키 "Ctrl+Alt+L"을 직접 사용하여 코드 형식을 지정합니다. 2. 아이디어를 열고 "파일" - "설정" - "플러그인"을 클릭하고 "eclipse 코드 포맷터"를 검색하여 설치한 다음 "eclipse-codestyle.xml" 구성 파일을 가져오고 마지막으로 단축키 "Ctrl+Shift"를 사용합니다. +Alt" +L" 형식 코드.
이 튜토리얼의 운영 환경: windows10 시스템, IDEA2020.1.3, Dell G3 컴퓨터.
IDEA(IntelliJ IDEA의 전체 이름)는 Java 프로그래밍 언어를 위한 통합 개발 환경입니다. IntelliJ는 특히 지능형 코드 도우미, 자동 코드 프롬프트, 리팩토링, JavaEE 지원, 다양한 버전 도구(git, svn 등), JUnit, CVS 통합, 코드 분석, 혁신적인 분야에서 업계 최고의 Java 개발 도구로 인정받고 있습니다. GUI 디자인 및 기타 측면의 기능은 특별하다고 할 수 있습니다. IDEA는 체코 공화국의 수도 프라하에 본사를 둔 회사인 JetBrains의 제품입니다. 개발자는 주로 엄격함으로 유명한 동유럽 프로그래머입니다. 주력 버전은 HTML, CSS, PHP, MySQL, Python 등도 지원합니다. 무료 버전은 Java, Kotlin 등 일부 언어만 지원합니다.
방법 1: 기본 방법
IDEA에서는 Ctrl+Alt+L 단축키를 사용하여 코드 서식을 지정할 수 있습니다.
Ctrl+Alt+L 단축키를 사용하여 코드 서식을 지정하면 코드 서식이 깔끔해집니다.
방법 2: 플러그인 사용
단축키 "Ctrl+Alt+L"을 사용한 후 코드가 거의 변경되지 않고 형식화된 결과가 원하는 결과를 얻지 못하는 상황을 겪어본 적이 있습니까? 예상되는 효과는 무엇입니까? 구성 코드 형식 지정 템플릿을 사용해 볼 수도 있습니다.
1. 플러그인 설치: EclipseCodeFormatter
Alibaba 구성 파일은 Eclipse 전용이므로 구성 파일을 가져오기 전에 EclipseFormatter 플러그인을 설치해야 합니다.
아이디어 편집기를 열고: 파일->설정->플러그인
에서 Eclipse 코드 포맷터
를 검색하세요. File->Settings->Plugins
,搜索 eclipse code formatter
。
我这是已经安装好的页面
2、导入配置文件 eclipse-codestyle.xml
上面步骤操作完页面不要关,找到下面的Other Settings->Adapter eclipse code formatter
路径,按照图片位置找到eclipse-codestyle.xml
文件保存的位置,添加进去就行了(文件放文章后面
)
只格式化自己写的代码,不格式化版本仓库里面的代码,防止代码评审或者代码对比出现大量不一致的代码
快捷键:Ctrl+Shift+Alt+L
eclipse-codestyle.xml
2. 구성 파일 eclipse-codestyle.xml
을 가져옵니다
기타 설정->Adapter eclipse code formatter
경로에서 이미지 위치에 따라 eclipse-codestyle.xml
파일이 저장된 위치를 찾아 추가하면 됩니다( 파일은 기사 마지막 부분에 넣으세요
)🎜🎜 🎜🎜🎜🎜 2. 형식 지정 코드는 내가 수정한 코드만 형식화하고 수정되지 않은 코드는 형식화하지 않습니다. 🎜🎜🎜형식만 지정합니다. 내가 작성한 코드는 코드 리뷰나 코드 비교에서 일관되지 않은 코드가 많이 나타나는 것을 방지하기 위해 버전 웨어하우스에서 코드의 형식을 지정하지 않습니다.eclipse-codestyle.xml
파일은 자체 선택됩니다. 키 3개 연속 0.0🎜🎜🎜<?xml version="1.0" encoding="UTF-8" standalone="no"?> <profiles> <profile> <!--可变参数的... Idea没有对应的配置项,强制insert--> <setting></setting> <!--枚举值之间 Idea没有对应的配置项,强制insert--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_invocation_arguments=Java:SPACE_BEFORE_COMMA--> <!--org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_parameters=Java:SPACE_BEFORE_COMMA 由于IDEA只有一个SPACE_BEFORE_COMMA选项,所以统一设置 insert_space_before_comma 为 do not insert --> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--insert_space_before_comma end--> <!--org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments=Java:SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS--> <setting></setting> <!--IDEA只有一个配置项SPACE_AFTER_COMMA,insert_space_after_comma*统一设置成insert--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--insert_space_after_comma end--> <!--org.eclipse.jdt.core.formatter.insert_space_before_colon_in_conditional=Java:SPACE_BEFORE_COLON--> <setting></setting> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_after_colon_in_conditional=Java:SPACE_AFTER_COLON--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--IDEA不支持配置,默认do not insert--> <setting></setting> <setting></setting> <setting></setting> <!--这个在Eclipse也没有找到配置的地方--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_semicolon=Java:SPACE_BEFORE_SEMICOLON 程序导入的时候强制将SPACE_BEFORE_SEMICOLON设置为false --> <setting></setting> <setting></setting> <setting></setting> <!--SPACE_AFTER_SEMICOLON=true--> <setting></setting> <setting></setting> <!--IDEA不支持配置,do not insert--> <setting></setting> <setting></setting> <setting></setting> <!--IDEA不支持,使用默认--> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_after_binary_operator=Java:<Programmatic>--> <setting></setting> <setting></setting> <!--IDEA不支持配置,使用如下值,两者对应--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--Java:SPACE_BEFORE_OPENING_ANGLE_BRACKET_IN_TYPE_PARAMETER--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments=Java:SPACE_AFTER_CLOSING_ANGLE_BRACKET_IN_TYPE_ARGUMENT--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block=Java:<Programmatic>--> <setting></setting> <!--IDEA使用了对应的配置:Java:SPACE_WITHIN_ARRAY_INITIALIZER_BRACES,但感觉不太好,IDEA默认不插入,Eclipse也使用不插入--> <setting></setting> <!--use default insert--> <setting></setting> <!--use default do not insert --> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--use default insert--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch=Java:SPACE_BEFORE_SWITCH_LBRACE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration=Java:SPACE_BEFORE_CLASS_LBRACE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_block=Java:<Programmatic>--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_array_initializer=Java:SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_method_declaration=Java:SPACE_BEFORE_METHOD_LBRACE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=Java:SPACE_AFTER_QUEST--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional=Java:SPACE_BEFORE_QUEST--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation=Java:SPACE_BEFORE_ANOTATION_PARAMETER_LIST--> <setting></setting> <!--use default do not insert--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--下面两个对应IDEA中的一个配置Java:SPACE_AROUND_ASSIGNMENT_OPERATORS,使用insert--> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_catch=Java:SPACE_BEFORE_CATCH_PARENTHESES--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_invocation=Java:SPACE_BEFORE_METHOD_CALL_PARENTHESES--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try=Java:SPACE_BEFORE_TRY_PARENTHESES--> <setting></setting> <!--下面两个对应IDEA中的一个配置Java:SPACE_AROUND_UNARY_OPERATOR,使用do not insert--> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_if=Java:SPACE_BEFORE_IF_PARENTHESES--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while=Java:SPACE_BEFORE_WHILE_PARENTHESES--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_after_closing_paren_in_cast=Java:SPACE_AFTER_TYPE_CAST--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_declaration=Java:SPACE_BEFORE_METHOD_PARENTHESES--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_for=Java:SPACE_BEFORE_FOR_PARENTHESES--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_synchronized=Java:SPACE_BEFORE_SYNCHRONIZED_PARENTHESES--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch=Java:SPACE_BEFORE_SWITCH_PARENTHESES--> <setting></setting> <!--下面两个对应IDEA中的一个配置Java:SPACE_AROUND_LAMBDA_ARROW,使用insert--> <setting></setting> <setting></setting> <!--SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES--> <setting></setting> <!--Idea -> Wrapping And Braces -> Simple classes in one line --> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--Idea -> Wrapping And Braces -> Simple method in one line --> <setting></setting> <!--因为Idea不支持配置,所以设置为 Idea默认值--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--Idea可以通过Wrap Always实现 TODO--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--Idea -> Wrapping And Braces -> Simple block in one line -> do not select --> <setting></setting> <!--Idea -> Wrapping And Braces -> try statement -> catch.... (Java:CATCH_ON_NEW_LINE)--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=<Programmatic>--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer=Java:ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE--> <setting></setting> <!--#org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=Java:ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement=Java:ELSE_ON_NEW_LINE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_new_line_before_while_in_do_statement=Java:WHILE_ON_NEW_LINE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement=Java:FINALLY_ON_NEW_LINE--> <setting></setting> <!--comment start--> <setting></setting> <!--ENABLE_JAVADOC_FORMATTING--> <setting></setting> <!--org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment=<Programmatic>--> <setting></setting> <!--IDEA无对应设置,所以关闭对block comment的格式化 --> <setting></setting> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=Java:KEEP_FIRST_COLUMN_COMMENT--> <setting></setting> <!--org.eclipse.jdt.core.formatter.use_on_off_tags=FORMATTER_TAGS_ENABLED--> <setting></setting> <!--org.eclipse.jdt.core.formatter.disabling_tag=FORMATTER_OFF_TAG--> <setting></setting> <!--org.eclipse.jdt.core.formatter.enabling_tag=FORMATTER_ON_TAG--> <setting></setting> <!--下面的没有IDEA对应项,在代码里面对IDEA中使用默认值即可,LINE_COMMENT_AT_FIRST_COLUMN BLOCK_COMMENT_AT_FIRST_COLUMN设置为false--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--和IDEA保持一致,注释换行--> <setting></setting> <setting></setting> <!--comment end--> <!--org.eclipse.jdt.core.formatter.blank_lines_after_imports=Java:BLANK_LINES_AFTER_IMPORTS--> <setting></setting> <!--org.eclipse.jdt.core.formatter.blank_lines_before_imports=Java:BLANK_LINES_BEFORE_IMPORTS--> <setting></setting> <!--org.eclipse.jdt.core.formatter.blank_lines_after_package=Java:BLANK_LINES_AFTER_PACKAGE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.blank_lines_between_type_declarations=Java:BLANK_LINES_AROUND_CLASS--> <setting></setting> <!--org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=Java:BLANK_LINES_BEFORE_METHOD_BODY--> <setting></setting> <!--org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration=Java:<Programmatic>--> <setting></setting> <!--org.eclipse.jdt.core.formatter.blank_lines_before_field=Java:BLANK_LINES_AROUND_FIELD--> <setting></setting> <!--org.eclipse.jdt.core.formatter.blank_lines_before_method=Java:BLANK_LINES_AROUND_METHOD--> <setting></setting> <!--org.eclipse.jdt.core.formatter.blank_lines_before_package=Java:BLANK_LINES_BEFORE_PACKAGE--> <setting></setting> <!--下面IDEA没有对应设置,使用对应值即可--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.indentation.size=Java:IndentOptions:INDENT_SIZE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.continuation_indentation=Java:IndentOptions:<Programmatic>--> <setting></setting> <!--org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header=Java:<Programmatic>--> <setting></setting> <!--org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=Java:IndentOptions:SMART_TABS--> <setting></setting> <!--org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=Java:INDENT_CASE_FROM_SWITCH--> <setting></setting> <!--KEEP_INDENTS_ON_EMPTY_LINES--> <setting></setting> <!--org.eclipse.jdt.core.formatter.tabulation.size=Java:IndentOptions:<Programmatic>--> <setting></setting> <!--Java:IndentOptions:<Programmatic>--> <setting></setting> <!--下面IDEA没有对应设置,使用对应值即可--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--Java:<Programmatic>--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--下面没有对应的IDEA设置,Eclipse先使用对应值--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--IDEA默认配置在同一行,Eclipse使用对应值即可--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--Java:BINARY_OPERATION_SIGN_ON_NEXT_LINE--> <setting></setting> <!--ASSIGNMENT_WRAP 需要设置为 WRAP_AS_NEEDED WRAP_AS_NEEDED . Add in jdt.core-3.12,it's not work in previous version --> <setting></setting> <!--IDEA无配置项,Eclipse使用对应值即可--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=Java:KEEP_CONTROL_STATEMENT_IN_ONE_LINE--> <setting></setting> <!--org.eclipse.jdt.core.formatter.compact_else_if=Java:SPECIAL_ELSE_IF_TREATMENT--> <setting></setting> <!--Java:ALIGN_GROUP_FIELD_DECLARATIONS--> <setting></setting> <!--Java:<Programmatic>--> <setting></setting> <setting></setting> <!--统一为end_of_lint,IDEA默认一致--> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <setting></setting> <!-- <setting id="org.eclipse.jdt.core.compiler.source" value="1.8"/> <setting id="org.eclipse.jdt.core.compiler.compliance" value="1.8"/> <setting id="org.eclipse.jdt.core.compiler.codegen.targetPlatform" value="1.8"/> <setting id="org.eclipse.jdt.core.compiler.problem.enumIdentifier" value="error"/> <setting id="org.eclipse.jdt.core.compiler.problem.assertIdentifier" value="error"/> <setting id="org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode" value="enabled"/> --> <!--Java:KEEP_SIMPLE_BLOCKS_IN_ONE_LINE--> <setting></setting> <!--Java:CLASS_BRACE_STYLE,统一使用end_of_line TODO--> <setting></setting> <setting></setting> <!--org.eclipse.jdt.core.formatter.lineSplit=RIGHT_MARGIN--> <setting></setting> </profile> </profiles>🎜더 많은 관련 지식은 🎜FAQ🎜 칼럼을 방문해주세요! 🎜
위 내용은 아이디어에서 코드 형식을 지정하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!