[bytecode viewer] fix line number when caret is on leading whitespace

GitOrigin-RevId: 753378e6f5a4f05be7113f89d74ba699bab5f07d
This commit is contained in:
Vladimir Parfinenko
2023-04-13 12:46:25 +02:00
committed by intellij-monorepo-bot
parent a633c2f27c
commit 4c7ac8a0ba
3 changed files with 18 additions and 10 deletions

View File

@@ -68,13 +68,18 @@ public class ByteCodeViewerComponent extends JPanel implements Disposable {
setText(bytecode, 0);
}
public void setText(@NonNls final String bytecode, PsiElement element) {
public void setText(@NonNls final String bytecode, PsiElement element, int lineNumber) {
int offset = -1;
VirtualFile file = PsiUtilCore.getVirtualFile(element);
if (file != null) {
final Document document = FileDocumentManager.getInstance().getDocument(file);
if (document != null) {
int lineNumber = document.getLineNumber(element.getTextOffset()) + 1;
if (lineNumber == -1) {
lineNumber = document.getLineNumber(element.getTextOffset());
}
// Use 1-based line numbers from here:
lineNumber++;
LineNumbersMapping mapping = file.getUserData(LineNumbersMapping.LINE_NUMBERS_MAPPING_KEY);
if (mapping != null) {

View File

@@ -106,12 +106,12 @@ public final class ByteCodeViewerManager extends DockablePopupManager<ByteCodeVi
}
private void updateByteCode(PsiElement element, ByteCodeViewerComponent component, Content content) {
updateByteCode(element, component, content, getByteCode(element));
updateByteCode(element, -1, component, content, getByteCode(element));
}
private void updateByteCode(PsiElement element, ByteCodeViewerComponent component, Content content, String byteCode) {
private void updateByteCode(PsiElement element, int lineNumber, ByteCodeViewerComponent component, Content content, String byteCode) {
if (!StringUtil.isEmpty(byteCode)) {
component.setText(byteCode, element);
component.setText(byteCode, element, lineNumber);
}
else {
PsiElement presentableElement = getContainingClass(element);
@@ -155,13 +155,13 @@ public final class ByteCodeViewerManager extends DockablePopupManager<ByteCodeVi
@Override
protected void doUpdateComponent(@NotNull PsiElement element) {
doUpdateComponent(element, getByteCode(element));
doUpdateComponent(element, -1, getByteCode(element));
}
void doUpdateComponent(@NotNull PsiElement element, final String newText) {
void doUpdateComponent(@NotNull PsiElement element, int lineNumber, final String newText) {
Content content = myToolWindow.getContentManager().getSelectedContent();
if (content != null) {
updateByteCode(element, (ByteCodeViewerComponent)content.getComponent(), content, newText);
updateByteCode(element, lineNumber, (ByteCodeViewerComponent)content.getComponent(), content, newText);
}
}

View File

@@ -65,6 +65,9 @@ final class ShowByteCodeAction extends AnAction {
final PsiElement psiElement = getPsiElement(dataContext, project, editor);
if (psiElement == null) return;
// Some PSI elements could be multiline. Try to be precise about the line we were invoked at.
int lineNumber = editor != null ? editor.getCaretModel().getLogicalPosition().line : -1;
if (ByteCodeViewerManager.getContainingClass(psiElement) == null) {
Messages.showWarningDialog(project, JavaByteCodeViewerBundle.message("bytecode.class.in.selection.message"),
JavaByteCodeViewerBundle.message("bytecode.not.found.message"));
@@ -104,7 +107,7 @@ final class ShowByteCodeAction extends AnAction {
final ByteCodeViewerManager codeViewerManager = ByteCodeViewerManager.getInstance(project);
if (codeViewerManager.hasActiveDockedDocWindow()) {
codeViewerManager.doUpdateComponent(targetElement, myByteCode);
codeViewerManager.doUpdateComponent(targetElement, lineNumber, myByteCode);
}
else {
if (myByteCode == null) {
@@ -114,7 +117,7 @@ final class ShowByteCodeAction extends AnAction {
}
final ByteCodeViewerComponent component = new ByteCodeViewerComponent(project);
component.setText(myByteCode, targetElement);
component.setText(myByteCode, targetElement, lineNumber);
Processor<JBPopup> pinCallback = popup -> {
codeViewerManager.recreateToolWindow(targetElement, targetElement);
popup.cancel();