Skip to content

Show original and changed linefeed in diff view #8645

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: master
Choose a base branch
from
Open
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
Show original and changed linefeed in diff view
  • Loading branch information
Chris2011 committed Jul 12, 2025
commit 273527e46e03b19f6a29987bc732cc0c9f44bcff
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.editor.settings.FontColorNames;
import org.netbeans.api.editor.settings.FontColorSettings;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.BaseTextUI;
import org.netbeans.spi.diff.DiffProvider;
import org.netbeans.spi.diff.DiffControllerImpl;
import org.netbeans.editor.EditorUI;
import org.netbeans.lib.editor.util.swing.DocumentUtilities;
import org.netbeans.modules.diff.builtin.visualizer.TextDiffVisualizer;
import static org.netbeans.modules.editor.errorstripe.privatespi.MarkProvider.PROP_MARKS;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.text.NbDocument;
Expand Down Expand Up @@ -122,13 +124,18 @@ public class EditableDiffView extends DiffControllerImpl implements DiffView, Do

final JLabel fileLabel1 = new JLabel();
final JLabel fileLabel2 = new JLabel();
final JLabel lineEndingLabel1 = new JLabel();
final JLabel lineEndingLabel2 = new JLabel();
final JPanel filePanel1 = new JPanel();
final JPanel filePanel2 = new JPanel();
final JPanel textualPanel = new JPanel();
final JTabbedPane jTabbedPane;
final JComponent view;
final JSplitPane jSplitPane1 = new JSplitPane();

private String lineEnding1;
private String lineEnding2;

private int diffSerial;
private Difference[] diffs = NO_DIFFERENCES;

Expand Down Expand Up @@ -166,6 +173,12 @@ public class EditableDiffView extends DiffControllerImpl implements DiffView, Do
private final Object DIFFING_LOCK = new Object();
private final String name1;
private final String name2;

private final StreamSource source1;
private final StreamSource source2;
private final String title1;
private final String title2;

private boolean sourcesInitialized;
private boolean viewAdded;
private boolean addedToHierarchy;
Expand All @@ -177,10 +190,17 @@ public EditableDiffView (final StreamSource ss1, final StreamSource ss2) {
public EditableDiffView(final StreamSource ss1, final StreamSource ss2, boolean enhancedView) {
refreshDiffTask = rp.create(new RefreshDiffTask());
initColors();
String title1 = ss1.getTitle();
if (title1 == null) title1 = NbBundle.getMessage(EditableDiffView.class, "CTL_DiffPanel_NoTitle"); // NOI18N
String title2 = ss2.getTitle();
if (title2 == null) title2 = NbBundle.getMessage(EditableDiffView.class, "CTL_DiffPanel_NoTitle"); // NOI18N

source1 = ss1;
source2 = ss2;

String t1 = ss1.getTitle();
if (t1 == null) t1 = NbBundle.getMessage(EditableDiffView.class, "CTL_DiffPanel_NoTitle"); // NOI18N
String t2 = ss2.getTitle();
if (t2 == null) t2 = NbBundle.getMessage(EditableDiffView.class, "CTL_DiffPanel_NoTitle"); // NOI18N
title1 = t1;
title2 = t2;

String mimeType1 = ss1.getMIMEType();
String mimeType2 = ss2.getMIMEType();
name1 = ss1.getName();
Expand Down Expand Up @@ -237,7 +257,7 @@ public void removeNotify () {
view.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(EditableDiffView.class, "ACS_DiffPanelA11yName")); // NOI18N
view.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(EditableDiffView.class, "ACS_DiffPanelA11yDesc")); // NOI18N
initializeTabPane(ss1, ss2);

setSourceTitle(fileLabel1, title1);
setSourceTitle(fileLabel2, title2);

Expand Down Expand Up @@ -372,6 +392,38 @@ public void run () {
}
}

private static String detectLineEnding(StreamSource source) {
try {
FileObject fo = source.getLookup().lookup(FileObject.class);
DataObject dao = DataObject.find(fo);

EditorCookie editorCookie = dao.getCookie(EditorCookie.class);
if (editorCookie == null) {
return null;
}

Document doc;
try {
doc = editorCookie.openDocument();
} catch (IOException ex) {
return null;
}

String separator = doc.getProperty(BaseDocument.READ_LINE_SEPARATOR_PROP).toString();
if ("\n".equals(separator)) {
return "LF";
} else if ("\r\n".equals(separator)) {
return "CRLF";
} else if ("\r".equals(separator)) {
return "CR";
}
} catch (Exception e) {
// fallback to other means of obtaining the source
}

return null;
}

private void initializeTabPane (StreamSource ss1, StreamSource ss2) {
if (jTabbedPane != null) {
jTabbedPane.addTab(org.openide.util.NbBundle.getMessage(EditableDiffView.class, "EditableDiffView.viewGraphical.title"), jSplitPane1); //NOI18N
Expand Down Expand Up @@ -939,19 +991,36 @@ public void run () {
// scroll the left pane accordingly
manager.scroll(index == diffs.length - 1 || index == 0);
}

/** This method is called from within the constructor to initialize the form.
*/
private void initComponents() {
fileLabel1.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
fileLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lineEndingLabel1.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
lineEndingLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
JPanel headerPanel1 = new JPanel(new BorderLayout());

JLabel filler1 = new JLabel();
headerPanel1.add(filler1, BorderLayout.WEST);
headerPanel1.add(fileLabel1, BorderLayout.CENTER);
headerPanel1.add(lineEndingLabel1, BorderLayout.EAST);
filePanel1.setLayout(new BorderLayout());
filePanel1.add(fileLabel1, BorderLayout.PAGE_START);
filePanel1.add(headerPanel1, BorderLayout.PAGE_START);

fileLabel2.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
fileLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lineEndingLabel2.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

lineEndingLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
JPanel headerPanel2 = new JPanel(new BorderLayout());

JLabel filler2 = new JLabel();
headerPanel2.add(lineEndingLabel2, BorderLayout.WEST);
headerPanel2.add(fileLabel2, BorderLayout.CENTER);
headerPanel2.add(filler2, BorderLayout.EAST);
filePanel2.setLayout(new BorderLayout());
filePanel2.add(fileLabel2, BorderLayout.PAGE_START);
filePanel2.add(headerPanel2, BorderLayout.PAGE_START);

textualPanel.setLayout(new BorderLayout());

Expand Down Expand Up @@ -1518,6 +1587,20 @@ public void run() {
support.firePropertyChange(DiffController.PROP_DIFFERENCES, null, null);
jEditorPane1.setCurrentDiff(diffs);
jEditorPane2.setCurrentDiff(diffs);

lineEnding1 = detectLineEnding(source1);
lineEnding2 = detectLineEnding(source2);

boolean showLineEnding = lineEnding1 != null && lineEnding2 != null && !lineEnding1.equals(lineEnding2);

if(showLineEnding) {
lineEndingLabel1.setText("<html><strong style='background-color: " + String.format("#%02x%02x%02x", colorChanged.getRed(), colorChanged.getGreen(), colorChanged.getBlue()) + "'>" + lineEnding1 + "</strong></html>");
lineEndingLabel2.setText("<html><strong style='background-color: " + String.format("#%02x%02x%02x", colorChanged.getRed(), colorChanged.getGreen(), colorChanged.getBlue()) + "'>" + lineEnding2 + "</strong></html>");
}

setSourceTitle(fileLabel1, title1);
setSourceTitle(fileLabel2, title2);

refreshDividerSize();
view.repaint();
diffMarkprovider.refresh();
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy