Skip to content

UnsupportedOperationException when creating 'scratch file' in IntelliJ IDEA #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public boolean canFormat(@NotNull PsiFile file) {
public void formatDocument(@NotNull Document document, @NotNull List<TextRange> formattingRanges,
@NotNull FormattingContext formattingContext, boolean canChangeWhiteSpaceOnly, boolean quickFormat) {
VirtualFile file = formattingContext.getVirtualFile();
Path path = (file != null) ? file.toNioPath() : null;
Path path = (file != null) ? file.getFileSystem().getNioPath(file) : null;
JavaFormatConfig config = JavaFormatConfig.findFrom(path);
Formatter formatter = new Formatter(config);
String source = document.getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.spring.format.formatter.intellij.formatting;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;

import com.intellij.formatting.FormattingContext;
Expand All @@ -25,16 +27,21 @@
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.PlainTextFileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.local.CoreLocalFileSystem;
import com.intellij.openapi.vfs.local.CoreLocalVirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.LightVirtualFile;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import io.spring.format.formatter.intellij.state.State;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
* Tests for {@link SpringJavaFormatFormattingService}.
Expand Down Expand Up @@ -72,14 +79,49 @@ void canFormatWhenJavaFileAndActiveReturnsTrue() {
assertThat(this.service.canFormat(file)).isTrue();
}

@Test
void formatDocumentAppliesFormatting(@TempDir Path projectDir) throws Exception {
Files.writeString(projectDir.resolve(".springjavaformatconfig"), "indentation-style=spaces");
Document document = mockDocument("public class Hello{"
+ "\tpublic void hello() {"
+ "\tString value =\t\"Hello World\";}}");
FormattingContext formattingContext = mock(FormattingContext.class);
VirtualFile virtualFile = new CoreLocalVirtualFile(new CoreLocalFileSystem(), projectDir.resolve("Hello.java"));
given(formattingContext.getVirtualFile()).willReturn(virtualFile);
this.service.formatDocument(document, Collections.emptyList(), formattingContext, false, false);
assertThat(document.getText()).isEqualTo("public class Hello {\n\n"
+ " public void hello() {\n"
+ " String value = \"Hello World\";\n"
+ " }\n\n"
+ "}");
}

@Test
void formatDocumentAppliesFormatting() {
Document document = mock(Document.class);
String text = "public class Hello {}";
given(document.getText()).willReturn(text);
Document document = mockDocument("public class Hello{"
+ "\tpublic void hello() {"
+ "\tString value =\t\"Hello World\";}}");
FormattingContext formattingContext = mock(FormattingContext.class);
VirtualFile virtualFile = new LightVirtualFile("Hello.java", document.getText());
given(formattingContext.getVirtualFile()).willReturn(virtualFile);
this.service.formatDocument(document, Collections.emptyList(), formattingContext, false, false);
verify(document).replaceString(20, 20, "\n\n");
assertThat(document.getText()).isEqualTo("public class Hello {\n\n"
+ "\tpublic void hello() {\n"
+ "\t\tString value = \"Hello World\";\n"
+ "\t}\n\n"
+ "}");
}


private Document mockDocument(String text) {
Document document = mock(Document.class);
StringBuilder documentText = new StringBuilder(text);
willAnswer((invocation) -> {
documentText.replace(invocation.getArgument(0), invocation.getArgument(1), invocation.getArgument(2));
return null;
}).given(document).replaceString(any(Integer.class), any(Integer.class), any(CharSequence.class));
given(document.getText()).willAnswer((invocation) -> documentText.toString());
return document;
}

private PsiFile mockFile(FileType fileType, State state) {
Expand Down