mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-09 01:05:38 +02:00
Bug 371165 - Invalid formatting of newly created classes.
This commit is contained in:
parent
ddab4b3f49
commit
6322c0691d
1 changed files with 46 additions and 42 deletions
|
@ -23,6 +23,7 @@ import org.eclipse.cdt.core.formatter.CodeFormatter;
|
||||||
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
|
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
|
||||||
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions;
|
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions;
|
||||||
import org.eclipse.cdt.core.index.IIndex;
|
import org.eclipse.cdt.core.index.IIndex;
|
||||||
|
import org.eclipse.cdt.core.model.CModelException;
|
||||||
import org.eclipse.cdt.core.model.CoreModel;
|
import org.eclipse.cdt.core.model.CoreModel;
|
||||||
import org.eclipse.cdt.core.model.ILanguage;
|
import org.eclipse.cdt.core.model.ILanguage;
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
|
@ -34,8 +35,8 @@ import org.eclipse.cdt.core.parser.ScannerInfo;
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IStatus;
|
import org.eclipse.core.runtime.IStatus;
|
||||||
import org.eclipse.text.edits.TextEdit;
|
|
||||||
import org.eclipse.jface.text.IRegion;
|
import org.eclipse.jface.text.IRegion;
|
||||||
|
import org.eclipse.text.edits.TextEdit;
|
||||||
|
|
||||||
public class CCodeFormatter extends CodeFormatter {
|
public class CCodeFormatter extends CodeFormatter {
|
||||||
private DefaultCodeFormatterOptions preferences;
|
private DefaultCodeFormatterOptions preferences;
|
||||||
|
@ -106,10 +107,10 @@ public class CCodeFormatter extends CodeFormatter {
|
||||||
if (options != null) {
|
if (options != null) {
|
||||||
this.options= options;
|
this.options= options;
|
||||||
Map<String, String> formatterPrefs= new HashMap<String, String>(options.size());
|
Map<String, String> formatterPrefs= new HashMap<String, String>(options.size());
|
||||||
for (String key : options.keySet()) {
|
for (Map.Entry<String, ?> entry : options.entrySet()) {
|
||||||
Object value= options.get(key);
|
Object value = entry.getValue();
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
formatterPrefs.put(key, (String) value);
|
formatterPrefs.put(entry.getKey(), (String) value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
preferences= new DefaultCodeFormatterOptions(formatterPrefs);
|
preferences= new DefaultCodeFormatterOptions(formatterPrefs);
|
||||||
|
@ -122,13 +123,6 @@ public class CCodeFormatter extends CodeFormatter {
|
||||||
@Override
|
@Override
|
||||||
public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) {
|
public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) {
|
||||||
TextEdit edit= null;
|
TextEdit edit= null;
|
||||||
ITranslationUnit tu= (ITranslationUnit) options.get(DefaultCodeFormatterConstants.FORMATTER_TRANSLATION_UNIT);
|
|
||||||
if (tu == null) {
|
|
||||||
IFile file= (IFile) options.get(DefaultCodeFormatterConstants.FORMATTER_CURRENT_FILE);
|
|
||||||
if (file != null) {
|
|
||||||
tu= (ITranslationUnit) CoreModel.getDefault().create(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lineSeparator != null) {
|
if (lineSeparator != null) {
|
||||||
preferences.line_separator = lineSeparator;
|
preferences.line_separator = lineSeparator;
|
||||||
} else {
|
} else {
|
||||||
|
@ -136,6 +130,7 @@ public class CCodeFormatter extends CodeFormatter {
|
||||||
}
|
}
|
||||||
preferences.initial_indentation_level = indentationLevel;
|
preferences.initial_indentation_level = indentationLevel;
|
||||||
|
|
||||||
|
ITranslationUnit tu = getTranslationUnit(source);
|
||||||
if (tu != null) {
|
if (tu != null) {
|
||||||
IIndex index;
|
IIndex index;
|
||||||
try {
|
try {
|
||||||
|
@ -149,7 +144,7 @@ public class CCodeFormatter extends CodeFormatter {
|
||||||
IASTTranslationUnit ast;
|
IASTTranslationUnit ast;
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
ast= tu.getAST(index, ITranslationUnit.AST_SKIP_ALL_HEADERS);
|
ast= tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
|
||||||
} catch (CoreException exc) {
|
} catch (CoreException exc) {
|
||||||
throw new AbortFormatting(exc);
|
throw new AbortFormatting(exc);
|
||||||
}
|
}
|
||||||
|
@ -191,19 +186,13 @@ public class CCodeFormatter extends CodeFormatter {
|
||||||
@Override
|
@Override
|
||||||
public TextEdit[] format(int kind, String source, IRegion[] regions, String lineSeparator) {
|
public TextEdit[] format(int kind, String source, IRegion[] regions, String lineSeparator) {
|
||||||
TextEdit[] edits= new TextEdit[regions.length];
|
TextEdit[] edits= new TextEdit[regions.length];
|
||||||
ITranslationUnit tu= (ITranslationUnit) options.get(DefaultCodeFormatterConstants.FORMATTER_TRANSLATION_UNIT);
|
|
||||||
if (tu == null) {
|
|
||||||
IFile file= (IFile) options.get(DefaultCodeFormatterConstants.FORMATTER_CURRENT_FILE);
|
|
||||||
if (file != null) {
|
|
||||||
tu= (ITranslationUnit) CoreModel.getDefault().create(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lineSeparator != null) {
|
if (lineSeparator != null) {
|
||||||
preferences.line_separator = lineSeparator;
|
preferences.line_separator = lineSeparator;
|
||||||
} else {
|
} else {
|
||||||
preferences.line_separator = System.getProperty("line.separator"); //$NON-NLS-1$
|
preferences.line_separator = System.getProperty("line.separator"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ITranslationUnit tu = getTranslationUnit(source);
|
||||||
if (tu != null) {
|
if (tu != null) {
|
||||||
IIndex index;
|
IIndex index;
|
||||||
try {
|
try {
|
||||||
|
@ -217,20 +206,11 @@ public class CCodeFormatter extends CodeFormatter {
|
||||||
IASTTranslationUnit ast;
|
IASTTranslationUnit ast;
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
ast= tu.getAST(index, ITranslationUnit.AST_SKIP_ALL_HEADERS);
|
ast= tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
|
||||||
} catch (CoreException exc) {
|
} catch (CoreException e) {
|
||||||
throw new AbortFormatting(exc);
|
throw new AbortFormatting(e);
|
||||||
}
|
|
||||||
for (int i = 0; i < regions.length; i++) {
|
|
||||||
IRegion region = regions[i];
|
|
||||||
CodeFormatterVisitor codeFormatter =
|
|
||||||
new CodeFormatterVisitor(preferences, region.getOffset(), region.getLength());
|
|
||||||
edits[i] = codeFormatter.format(source, ast);
|
|
||||||
IStatus status= codeFormatter.getStatus();
|
|
||||||
if (!status.isOK()) {
|
|
||||||
CCorePlugin.log(status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
formatRegions(source, regions, edits, ast);
|
||||||
} finally {
|
} finally {
|
||||||
index.releaseReadLock();
|
index.releaseReadLock();
|
||||||
}
|
}
|
||||||
|
@ -247,6 +227,16 @@ public class CCodeFormatter extends CodeFormatter {
|
||||||
try {
|
try {
|
||||||
ast= language.getASTTranslationUnit(content, scanInfo, contentProvider, null, 0,
|
ast= language.getASTTranslationUnit(content, scanInfo, contentProvider, null, 0,
|
||||||
ParserUtil.getParserLogService());
|
ParserUtil.getParserLogService());
|
||||||
|
formatRegions(source, regions, edits, ast);
|
||||||
|
} catch (CoreException e) {
|
||||||
|
throw new AbortFormatting(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return edits;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void formatRegions(String source, IRegion[] regions, TextEdit[] edits,
|
||||||
|
IASTTranslationUnit ast) {
|
||||||
for (int i = 0; i < regions.length; i++) {
|
for (int i = 0; i < regions.length; i++) {
|
||||||
IRegion region = regions[i];
|
IRegion region = regions[i];
|
||||||
CodeFormatterVisitor codeFormatter =
|
CodeFormatterVisitor codeFormatter =
|
||||||
|
@ -257,10 +247,24 @@ public class CCodeFormatter extends CodeFormatter {
|
||||||
CCorePlugin.log(status);
|
CCorePlugin.log(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (CoreException e) {
|
}
|
||||||
|
|
||||||
|
private ITranslationUnit getTranslationUnit(String source) {
|
||||||
|
ITranslationUnit tu= (ITranslationUnit) options.get(DefaultCodeFormatterConstants.FORMATTER_TRANSLATION_UNIT);
|
||||||
|
if (tu == null) {
|
||||||
|
IFile file= (IFile) options.get(DefaultCodeFormatterConstants.FORMATTER_CURRENT_FILE);
|
||||||
|
if (file != null) {
|
||||||
|
tu= (ITranslationUnit) CoreModel.getDefault().create(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (tu != null && source != null) {
|
||||||
|
try {
|
||||||
|
tu = tu.getWorkingCopy();
|
||||||
|
tu.getBuffer().setContents(source);
|
||||||
|
} catch (CModelException e) {
|
||||||
throw new AbortFormatting(e);
|
throw new AbortFormatting(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return edits;
|
return tu;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue