Merge remote-tracking branch 'origin/GP-473_DecompilerComments--SQUASHED'

This commit is contained in:
ghidra1 2021-02-01 18:06:31 -05:00
commit 165d9221c4
4 changed files with 91 additions and 35 deletions

View file

@ -595,11 +595,21 @@ void EmitPrettyPrint::overflow(void)
else else
break; break;
} }
int4 newspaceremain;
if (!indentstack.empty()) if (!indentstack.empty())
spaceremain = indentstack.back(); newspaceremain = indentstack.back();
else else
spaceremain = maxlinesize; newspaceremain = maxlinesize;
if (newspaceremain == spaceremain)
return; // Line breaking doesn't give us any additional space
if (commentmode && (newspaceremain == spaceremain + commentfill.size()))
return; // Line breaking doesn't give us any additional space
spaceremain = newspaceremain;
lowlevel->tagLine(maxlinesize-spaceremain); lowlevel->tagLine(maxlinesize-spaceremain);
if (commentmode &&(commentfill.size() != 0)) {
lowlevel->print(commentfill.c_str(),EmitXml::comment_color);
spaceremain -= commentfill.size();
}
} }
/// Content and markup is sent to the low-level emitter if appropriate. The /// Content and markup is sent to the low-level emitter if appropriate. The

View file

@ -44,7 +44,7 @@ public class ClangTextField extends WrappingVerticalLayoutTextField {
* *
* @param initialX The original x value passed to the constructor of this class * @param initialX The original x value passed to the constructor of this class
* @param lineNumberElement he line number element for this field from which we get a width * @param lineNumberElement he line number element for this field from which we get a width
* @return * @return the calculated offset
*/ */
private static int calculateXPositionWithLineNumberOffset(int initialX, private static int calculateXPositionWithLineNumberOffset(int initialX,
FieldElement lineNumberElement) { FieldElement lineNumberElement) {
@ -70,7 +70,7 @@ public class ClangTextField extends WrappingVerticalLayoutTextField {
FieldElement lineNumberFieldElement, int x, int width, HighlightFactory hlFactory) { FieldElement lineNumberFieldElement, int x, int width, HighlightFactory hlFactory) {
super(createSingleLineElement(fieldElements), super(createSingleLineElement(fieldElements),
calculateXPositionWithLineNumberOffset(x, lineNumberFieldElement), calculateXPositionWithLineNumberOffset(x, lineNumberFieldElement),
calculateWidthFromXPosition(x, lineNumberFieldElement, width), 30, hlFactory); calculateWidthFromXPosition(x, lineNumberFieldElement, width), 30, hlFactory, false);
this.tokenList = tokenList; this.tokenList = tokenList;
this.lineNumberFieldElement = lineNumberFieldElement; this.lineNumberFieldElement = lineNumberFieldElement;
} }
@ -157,8 +157,8 @@ public class ClangTextField extends WrappingVerticalLayoutTextField {
} }
@Override @Override
public void paint(JComponent c, Graphics g, PaintContext context, public void paint(JComponent c, Graphics g, PaintContext context, Rectangle clip,
Rectangle clip, FieldBackgroundColorManager selectionMap, RowColLocation cursorLoc, int rowHeight) { FieldBackgroundColorManager selectionMap, RowColLocation cursorLoc, int rowHeight) {
// Don't print line numbers; don't copy line numbers. We are assuming that the user only // Don't print line numbers; don't copy line numbers. We are assuming that the user only
// wants to copy code. // wants to copy code.

View file

@ -1,6 +1,5 @@
/* ### /* ###
* IP: GHIDRA * IP: GHIDRA
* REVIEWED: YES
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +17,6 @@ package docking.widgets.fieldpanel.field;
import docking.widgets.fieldpanel.support.*; import docking.widgets.fieldpanel.support.*;
public class WrappingVerticalLayoutTextField extends VerticalLayoutTextField { public class WrappingVerticalLayoutTextField extends VerticalLayoutTextField {
/** /**
@ -31,15 +29,27 @@ public class WrappingVerticalLayoutTextField extends VerticalLayoutTextField {
* @param maxLines the max number of lines to display * @param maxLines the max number of lines to display
* @param hlFactory the highlight factory * @param hlFactory the highlight factory
*/ */
public WrappingVerticalLayoutTextField(FieldElement textElement, public WrappingVerticalLayoutTextField(FieldElement textElement, int startX, int width,
int startX, int maxLines, HighlightFactory hlFactory) {
int width,
int maxLines,
HighlightFactory hlFactory) {
super(FieldUtils.wrap(textElement, width), startX, width, maxLines, hlFactory, ""); super(FieldUtils.wrap(textElement, width), startX, width, maxLines, hlFactory, "");
} }
/**
* Create a text field from a single FieldElement. The text is wrapped, either an words or simply
* @param textElement is the element to display
* @param startX is the position to draw the string
* @param width is the max width allocated to this field
* @param maxLines is the max number of lines to display
* @param hlFactory is the highlight factory
* @param breakOnWhiteSpace is true if wrapping should break on word boundaries
*/
public WrappingVerticalLayoutTextField(FieldElement textElement, int startX, int width,
int maxLines, HighlightFactory hlFactory, boolean breakOnWhiteSpace) {
super(FieldUtils.wrap(textElement, width, breakOnWhiteSpace), startX, width, maxLines,
hlFactory, "");
}
/** /**
* Finds the corresponding row, column for string index, and offset * Finds the corresponding row, column for string index, and offset
* @param index index into the string array * @param index index into the string array

View file

@ -31,7 +31,7 @@ public class FieldUtils {
} }
public static List<FieldElement> wrap(List<FieldElement> fieldElements, int width) { public static List<FieldElement> wrap(List<FieldElement> fieldElements, int width) {
List<FieldElement> wrappedElements = new ArrayList<FieldElement>(); List<FieldElement> wrappedElements = new ArrayList<>();
for (FieldElement fieldElement : fieldElements) { for (FieldElement fieldElement : fieldElements) {
wrappedElements.addAll(wordWrapList(fieldElement, width)); wrappedElements.addAll(wordWrapList(fieldElement, width));
} }
@ -52,7 +52,7 @@ public class FieldUtils {
return new FieldElement[] { originalFieldElement }; return new FieldElement[] { originalFieldElement };
} }
List<FieldElement> lines = new ArrayList<FieldElement>(); List<FieldElement> lines = new ArrayList<>();
int wordWrapPos = findWordWrapPosition(originalFieldElement, width); int wordWrapPos = findWordWrapPosition(originalFieldElement, width);
while (wordWrapPos > 0) { while (wordWrapPos > 0) {
lines.add(originalFieldElement.substring(0, wordWrapPos)); lines.add(originalFieldElement.substring(0, wordWrapPos));
@ -66,6 +66,42 @@ public class FieldUtils {
return lines.toArray(new FieldElement[lines.size()]); return lines.toArray(new FieldElement[lines.size()]);
} }
/**
* Splits the given FieldElement into sub-elements by wrapping the element in some fashion.
* If breakOnWhiteSpace is indicated, wrapping will break lines on a white space character
* if possible, otherwise wrapping occurs on the last possible character.
* @param fieldElement is the element to wrap
* @param width is the maximum width to allow before wrapping
* @param breakOnWhiteSpace determines whether line breaks should happen at white space chars
* @return the wrapped elements
*/
public static FieldElement[] wrap(FieldElement fieldElement, int width,
boolean breakOnWhiteSpace) {
if (breakOnWhiteSpace) {
return wrap(fieldElement, width);
}
FieldElement originalFieldElement = fieldElement.replaceAll(WHITE_SPACE, ' ');
if (originalFieldElement.getStringWidth() <= width) {
return new FieldElement[] { originalFieldElement };
}
List<FieldElement> lines = new ArrayList<>();
int wordWrapPos = originalFieldElement.getMaxCharactersForWidth(width);
if (wordWrapPos == originalFieldElement.length()) {
wordWrapPos = 0;
}
while (wordWrapPos > 0) {
lines.add(originalFieldElement.substring(0, wordWrapPos));
originalFieldElement = originalFieldElement.substring(wordWrapPos);
wordWrapPos = originalFieldElement.getMaxCharactersForWidth(width);
if (wordWrapPos == originalFieldElement.length()) {
wordWrapPos = 0;
}
}
lines.add(originalFieldElement);
return lines.toArray(new FieldElement[lines.size()]);
}
/** /**
* Splits the given FieldElement into sub-elements by wrapping the element on whitespace. * Splits the given FieldElement into sub-elements by wrapping the element on whitespace.
* *
@ -74,7 +110,7 @@ public class FieldUtils {
* @return The wrapped elements * @return The wrapped elements
*/ */
public static List<FieldElement> wordWrapList(FieldElement fieldElement, int width) { public static List<FieldElement> wordWrapList(FieldElement fieldElement, int width) {
List<FieldElement> lines = new ArrayList<FieldElement>(); List<FieldElement> lines = new ArrayList<>();
FieldElement originalFieldElement = fieldElement.replaceAll(WHITE_SPACE, ' '); FieldElement originalFieldElement = fieldElement.replaceAll(WHITE_SPACE, ' ');
if (originalFieldElement.getStringWidth() <= width) { if (originalFieldElement.getStringWidth() <= width) {