diff --git a/idea_plugin/build.gradle b/idea_plugin/build.gradle index a0b5c18ae..fe6469f25 100644 --- a/idea_plugin/build.gradle +++ b/idea_plugin/build.gradle @@ -24,45 +24,43 @@ task showProps { } } -subprojects { - repositories { - mavenCentral() - } +repositories { + mavenCentral() +} - ext { - googleJavaFormatVersion = '1.5' - } +ext { + googleJavaFormatVersion = '1.5' +} - apply plugin: 'org.jetbrains.intellij' - apply plugin: 'java' +apply plugin: 'org.jetbrains.intellij' +apply plugin: 'java' - intellij { - pluginName = "google-java-format" - updateSinceUntilBuild = true - sameSinceUntilBuild = true - } +intellij { + pluginName = "google-java-format" + updateSinceUntilBuild = true + sameSinceUntilBuild = true + version = "2017.3.3" +} - patchPluginXml { - pluginDescription = "Formats source code using the google-java-format tool. This version of " + - "the plugin uses version ${googleJavaFormatVersion} of the tool." + googleJavaFormatVersion - } +patchPluginXml { + pluginDescription = "Formats source code using the google-java-format tool. This version of " + + "the plugin uses version ${googleJavaFormatVersion} of the tool." + googleJavaFormatVersion + version = "${googleJavaFormatVersion}.1.171" + sinceBuild = '171' +} - publishPlugin { - username = project.ext.properties.jetbrainsPluginRepoUsername - password = project.ext.properties.jetbrainsPluginRepoPassword - } +publishPlugin { + username = project.ext.properties.jetbrainsPluginRepoUsername + password = project.ext.properties.jetbrainsPluginRepoPassword +} - sourceSets { - main { - java.srcDir '../src' - resources.srcDir '../resources' - } - test { - java.srcDir 'test' - } +sourceSets { + main { + java.srcDir 'src' + resources.srcDir 'resources' } +} - dependencies { - compile "com.google.googlejavaformat:google-java-format:${googleJavaFormatVersion}" - } +dependencies { + compile "com.google.googlejavaformat:google-java-format:${googleJavaFormatVersion}" } diff --git a/idea_plugin/settings.gradle b/idea_plugin/settings.gradle deleted file mode 100644 index f9456ba7e..000000000 --- a/idea_plugin/settings.gradle +++ /dev/null @@ -1 +0,0 @@ -include 'v162', 'v163', 'v171', 'v172', 'v173' diff --git a/idea_plugin/v163/src/CodeStyleManagerDecorator.java b/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java similarity index 77% rename from idea_plugin/v163/src/CodeStyleManagerDecorator.java rename to idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java index 4c7e83c07..43ec9f84d 100644 --- a/idea_plugin/v163/src/CodeStyleManagerDecorator.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/CodeStyleManagerDecorator.java @@ -16,6 +16,7 @@ package com.google.googlejavaformat.intellij; +import com.intellij.formatting.FormattingMode; import com.intellij.lang.ASTNode; import com.intellij.openapi.editor.Document; import com.intellij.openapi.fileTypes.FileType; @@ -26,6 +27,8 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.ChangedRangesInfo; import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.codeStyle.DocCommentSettings; +import com.intellij.psi.codeStyle.FormattingModeAwareIndentAdjuster; import com.intellij.psi.codeStyle.Indent; import com.intellij.util.IncorrectOperationException; import com.intellij.util.ThrowableRunnable; @@ -39,7 +42,9 @@ * @author bcsf@google.com (Brian Chang) */ @SuppressWarnings("deprecation") -class CodeStyleManagerDecorator extends CodeStyleManager { +class CodeStyleManagerDecorator extends CodeStyleManager + implements FormattingModeAwareIndentAdjuster { + private final CodeStyleManager delegate; CodeStyleManagerDecorator(CodeStyleManager delegate) { @@ -173,4 +178,43 @@ public void performActionWithFormatterDisabled(ThrowableRu public T performActionWithFormatterDisabled(Computable r) { return delegate.performActionWithFormatterDisabled(r); } + + @Override + public int getSpacing(PsiFile file, int offset) { + return delegate.getSpacing(file, offset); + } + + @Override + public int getMinLineFeeds(PsiFile file, int offset) { + return delegate.getMinLineFeeds(file, offset); + } + + /** Uses same fallback as {@link CodeStyleManager#getCurrentFormattingMode}. */ + @Override + public FormattingMode getCurrentFormattingMode() { + if (delegate instanceof FormattingModeAwareIndentAdjuster) { + return ((FormattingModeAwareIndentAdjuster) delegate).getCurrentFormattingMode(); + } + return FormattingMode.REFORMAT; + } + + @Override + public int adjustLineIndent(final Document document, final int offset, FormattingMode mode) + throws IncorrectOperationException { + if (delegate instanceof FormattingModeAwareIndentAdjuster) { + return ((FormattingModeAwareIndentAdjuster) delegate) + .adjustLineIndent(document, offset, mode); + } + return offset; + } + + @Override + public void runWithDocCommentFormattingDisabled(PsiFile file, Runnable runnable) { + delegate.runWithDocCommentFormattingDisabled(file, runnable); + } + + @Override + public DocCommentSettings getDocCommentSettings(PsiFile file) { + return delegate.getDocCommentSettings(file); + } } diff --git a/idea_plugin/test/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManagerTest.java b/idea_plugin/test/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManagerTest.java deleted file mode 100644 index 4fdca6b4c..000000000 --- a/idea_plugin/test/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManagerTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2015 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.googlejavaformat.intellij; - -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableList; -import com.google.googlejavaformat.java.JavaFormatterOptions; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.fileTypes.StdFileTypes; -import com.intellij.openapi.util.TextRange; -import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; - -/** - * Tests for {@link GoogleJavaFormatCodeStyleManager}. - * - * @author bcsf@google.com (Brian Chang) - */ -public class GoogleJavaFormatCodeStyleManagerTest extends LightCodeInsightFixtureTestCase { - - public void testFormatFile() { - CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject()); - final GoogleJavaFormatCodeStyleManager googleJavaFormat = - new BasicGoogleJavaFormatCodeStyleManager( - codeStyleManager, JavaFormatterOptions.Style.GOOGLE); - myFixture.configureByText( - StdFileTypes.JAVA, - "public class Test {public static void main(String[]args){System.out.println();}}"); - final int startOffset = 0; - final int endOffset = myFixture.getFile().getTextLength(); - ApplicationManager.getApplication() - .runWriteAction( - () -> googleJavaFormat.reformatText(myFixture.getFile(), startOffset, endOffset)); - myFixture.checkResult( - join( - "public class Test {", - " public static void main(String[] args) {", - " System.out.println();", - " }", - "}", - "")); - } - - public void testDontFormatNonJavaFile() { - CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject()); - final GoogleJavaFormatCodeStyleManager googleJavaFormat = - new BasicGoogleJavaFormatCodeStyleManager( - codeStyleManager, JavaFormatterOptions.Style.GOOGLE); - myFixture.configureByText( - StdFileTypes.PLAIN_TEXT, - "public class Test {public static void main(String[]args){System.out.println();}}"); - final int startOffset = 0; - final int endOffset = myFixture.getFile().getTextLength(); - ApplicationManager.getApplication() - .runWriteAction( - () -> googleJavaFormat.reformatText(myFixture.getFile(), startOffset, endOffset)); - myFixture.checkResult( - "public class Test {public static void main(String[]args){System.out.println();}}"); - } - - public void testFormatRanges() { - CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject()); - final GoogleJavaFormatCodeStyleManager googleJavaFormat = - new BasicGoogleJavaFormatCodeStyleManager( - codeStyleManager, JavaFormatterOptions.Style.GOOGLE); - String content = - join( - "public class Test {", // - " int a=0;", - " int b=1;", - " int c=2;", - "}"); - myFixture.configureByText(StdFileTypes.JAVA, content); - int aLineStart = content.indexOf("int a"); - int aLineEnd = content.indexOf("\n", aLineStart); - int cLineStart = content.indexOf("int c"); - int cLineEnd = content.indexOf("\n", cLineStart); - final ImmutableList ranges = - ImmutableList.of(new TextRange(aLineStart, aLineEnd), new TextRange(cLineStart, cLineEnd)); - ApplicationManager.getApplication() - .runWriteAction(() -> googleJavaFormat.reformatText(myFixture.getFile(), ranges)); - myFixture.checkResult( - join( - "public class Test {", // - " int a = 0;", - " int b=1;", - " int c = 2;", - "}")); - } - - private static String join(String... lines) { - return Joiner.on("\n").join(lines); - } -} diff --git a/idea_plugin/v163/build.gradle b/idea_plugin/v163/build.gradle deleted file mode 100644 index 93f341164..000000000 --- a/idea_plugin/v163/build.gradle +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2017 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -intellij { - version = '2016.3.7' -} - -patchPluginXml { - version = "${googleJavaFormatVersion}.0.163" - sinceBuild = '163' -} - -sourceSets { - main { - java.srcDir 'src' - } -}