From 94db642b5fab2ba1d2e184bde7bf07a357fe523e Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Wed, 14 May 2008 08:30:38 +0000 Subject: [PATCH] Let add includes use correct line-delimiter, bug 231580. --- .../AddIncludesOperation.java | 32 ++++++++++++++++--- .../internal/ui/editor/DocumentAdapter.java | 12 ++++++- .../ui/refactoring/DocumentAdapter.java | 2 ++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/AddIncludesOperation.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/AddIncludesOperation.java index b8b2c10c854..2f615ecde92 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/AddIncludesOperation.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/AddIncludesOperation.java @@ -18,10 +18,14 @@ import java.util.List; import org.eclipse.core.resources.IWorkspaceRunnable; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.jobs.ISchedulingRule; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.IBuffer; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.IInclude; @@ -44,8 +48,7 @@ public class AddIncludesOperation implements IWorkspaceRunnable { private ITranslationUnit fTranslationUnit; private IRequiredInclude[] fIncludes; private String[] fUsings; - - private String newLine = System.getProperty("line.separator", "\n"); //$NON-NLS-1$//$NON-NLS-2$ + private final String fNewLine; /** * Generate include statements for the passed java elements @@ -62,8 +65,27 @@ public class AddIncludesOperation implements IWorkspaceRunnable { fIncludes= includes; fUsings = using; fTranslationUnit = tu; + fNewLine= getNewLine(tu); } + private String getNewLine(ITranslationUnit tu) { + try { + IBuffer buf= tu.getBuffer(); + if (buf instanceof IAdaptable) { + IDocument doc= (IDocument) ((IAdaptable) buf).getAdapter(IDocument.class); + if (doc != null) { + String delim= doc.getLineDelimiter(0); + if (delim != null) { + return delim; + } + } + } + } catch (CModelException e) { + } catch (BadLocationException e) { + } + return System.getProperty("line.separator", "\n"); //$NON-NLS-1$//$NON-NLS-2$ } + } + public void executeIncludes(IProgressMonitor monitor) throws CoreException { // Sanity if (fIncludes == null || fIncludes.length == 0) { @@ -97,9 +119,9 @@ public class AddIncludesOperation implements IWorkspaceRunnable { for(int j = 0; j < toAdd.size(); j++) { IRequiredInclude req = toAdd.get(j); if (req.isStandard()) { - insert.append("#include <" + req.getIncludeName() + ">").append(newLine); //$NON-NLS-1$ //$NON-NLS-2$ + insert.append("#include <" + req.getIncludeName() + ">").append(fNewLine); //$NON-NLS-1$ //$NON-NLS-2$ } else { - insert.append("#include \"" + req.getIncludeName() + "\"").append(newLine); //$NON-NLS-1$ //$NON-NLS-2$ + insert.append("#include \"" + req.getIncludeName() + "\"").append(fNewLine); //$NON-NLS-1$ //$NON-NLS-2$ } } @@ -150,7 +172,7 @@ public class AddIncludesOperation implements IWorkspaceRunnable { StringBuffer insert = new StringBuffer(""); //$NON-NLS-1$ for(int j = 0; j < toAdd.size(); j++) { String using = toAdd.get(j); - insert.append("using namespace " + using + ";").append(newLine); //$NON-NLS-1$ //$NON-NLS-2$ + insert.append("using namespace " + using + ";").append(fNewLine); //$NON-NLS-1$ //$NON-NLS-2$ } int pos; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DocumentAdapter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DocumentAdapter.java index 31c6f4ec032..7cd1dbd23b3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DocumentAdapter.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DocumentAdapter.java @@ -30,6 +30,7 @@ import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourceAttributes; import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; @@ -58,7 +59,7 @@ import org.eclipse.cdt.ui.CUIPlugin; * * This class is similar to the JDT DocumentAdapter class. */ -public class DocumentAdapter implements IBuffer, IDocumentListener { +public class DocumentAdapter implements IBuffer, IDocumentListener, IAdaptable { /** * Internal implementation of a NULL instanceof IBuffer. @@ -543,4 +544,13 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { } } + @SuppressWarnings("unchecked") + public Object getAdapter(Class adapter) { + if (adapter.isAssignableFrom(ITextFileBuffer.class)) { + return fTextFileBuffer; + } else if (adapter.isAssignableFrom(IDocument.class)) { + return fDocument; + } + return null; + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/DocumentAdapter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/DocumentAdapter.java index 03edd5320b0..cabc5b52912 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/DocumentAdapter.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/DocumentAdapter.java @@ -487,6 +487,8 @@ public class DocumentAdapter implements IBuffer, IAdaptable, IDocumentListener { public Object getAdapter(Class adapter) { if (adapter.isAssignableFrom(ITextFileBuffer.class)) { return fTextFileBuffer; + } else if (adapter.isAssignableFrom(IDocument.class)) { + return fDocument; } return null; }