mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Let add includes use correct line-delimiter, bug 231580.
This commit is contained in:
parent
e836c2d3f5
commit
94db642b5f
3 changed files with 40 additions and 6 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue