Skip to content
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

Extract markdown contents for diff view #19

Merged
Merged
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
11 changes: 6 additions & 5 deletions src/main/java/com/zhongan/devpilot/util/DocumentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public static void insertCommentAndFormat(Project project, Editor editor, String
Document document = editor.getDocument();
int caretOffset = editor.getSelectionModel().getSelectionStart();
document.replaceString(editor.getSelectionModel().getSelectionStart(),
editor.getSelectionModel().getSelectionEnd(),
result);
editor.getSelectionModel().getSelectionEnd(),
result);
// format code
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
VirtualFile file = FileDocumentManager.getInstance().getFile(editor.getDocument());
codeStyleManager.reformatText(PsiManager.getInstance(project).findFile(file),
caretOffset, caretOffset + result.length());
caretOffset, caretOffset + result.length());
}));
}

Expand All @@ -58,19 +58,20 @@ public static void insertCommentAndFormat(Project project, Editor editor, String
* @param result
*/
public static void diffCommentAndFormatWindow(Project project, Editor editor, String result) {
String finalResult = MarkdownUtil.extractContents(result);
var selectionModel = editor.getSelectionModel();
ApplicationManager.getApplication().invokeLater(() -> WriteCommandAction.runWriteCommandAction(project, () -> {
VirtualFile createdFile = VirtualFileUtil.createVirtualReplaceFile(editor, project);
Document replaceDocument = FileDocumentManager.getInstance().getDocument(createdFile);

replaceDocument.setText(editor.getDocument().getText());
replaceDocument.setReadOnly(false);
replaceDocument.replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), result);
replaceDocument.replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), finalResult);

// auto code format
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformatText(PsiDocumentManager.getInstance(project).getPsiFile(replaceDocument),
selectionModel.getSelectionStart(), selectionModel.getSelectionStart() + result.length());
selectionModel.getSelectionStart(), selectionModel.getSelectionStart() + finalResult.length());
// show diff
PerformanceCheckUtils.showDiff(project, editor, FileDocumentManager.getInstance().getFile(editor.getDocument()), replaceDocument);
}));
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/zhongan/devpilot/util/MarkdownUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vladsch.flexmark.ast.FencedCodeBlock;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
Expand All @@ -17,6 +18,9 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

public class MarkdownUtil {

private static final String DEFAULT_CODE_FILE_EXTENSION = ".java";
Expand Down Expand Up @@ -53,6 +57,25 @@ public static String textContent2Html(String markdownText) {
.render(document);
}

public static String extractContents(String codeBlock) {
List<String> blocks = divideMarkdown(codeBlock);
List<String> contents = new ArrayList<>();
for (String block : blocks) {
if (block.startsWith("```")) {
com.vladsch.flexmark.util.ast.Document parse = Parser.builder().build().parse(codeBlock);
FencedCodeBlock codeNode = (FencedCodeBlock) parse.getChildOfType(FencedCodeBlock.class);
if (codeNode == null) {
return null;
}
contents.add(codeNode.getContentChars().unescape().replaceAll("\\n$", ""));
}
}
if (CollectionUtils.isEmpty(contents)) {
return codeBlock;
}
return StringUtils.join(contents, "\n\n");
}

private static final Map<String, String> languageFileExtMap = buildLanguageFileExtMap();

private static Map<String, String> buildLanguageFileExtMap() {
Expand Down