Skip to content

Commit

Permalink
fix(gui): improve detection of multi line method comments for update
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Dec 20, 2024
1 parent 8345edf commit fe0ab5e
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions jadx-gui/src/main/java/jadx/gui/ui/codearea/CommentAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import javax.swing.event.PopupMenuEvent;

import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenTypes;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -57,6 +59,12 @@ public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
}
}

@Override
public void popupMenuCanceled(PopupMenuEvent e) {
actionComment = null;
setEnabled(false);
}

private boolean updateCommentAction(int pos) {
ICodeComment codeComment = getCommentRef(pos);
if (codeComment == null) {
Expand Down Expand Up @@ -151,8 +159,7 @@ private ICodeComment getCommentRef(int pos) {
}

// check if at comment above node definition
String lineStr = codeArea.getLineAt(pos).trim();
if (lineStr.startsWith("//") || lineStr.startsWith("/*")) {
if (isCommentLine(pos)) {
ICodeNodeRef nodeRef = metadata.searchDown(pos, (off, ann) -> {
if (off > pos && ann.getAnnType() == AnnType.DECLARATION) {
return ((NodeDeclareRef) ann).getNode();
Expand All @@ -169,4 +176,34 @@ private ICodeComment getCommentRef(int pos) {
}
return null;
}

/**
* Check if all tokens are 'comment' in line at 'pos'
*/
private boolean isCommentLine(int pos) {
try {
int line = codeArea.getLineOfOffset(pos);
Token lineTokens = codeArea.getTokenListForLine(line);
boolean commentFound = false;
for (Token t = lineTokens; t != null; t = t.getNextToken()) {
if (t.isComment()) {
commentFound = true;
} else {
switch (t.getType()) {
case TokenTypes.WHITESPACE:
case TokenTypes.NULL:
// allowed tokens
break;

default:
return false;
}
}
}
return commentFound;
} catch (Exception e) {
LOG.warn("Failed to check for comment line", e);
return false;
}
}
}

0 comments on commit fe0ab5e

Please sign in to comment.