mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-01 21:35:40 +02:00
Code streamlining.
This commit is contained in:
parent
2fd430eb63
commit
4b09a45f5f
1 changed files with 7 additions and 62 deletions
|
@ -37,6 +37,7 @@ 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.jface.text.IRegion;
|
import org.eclipse.jface.text.IRegion;
|
||||||
|
import org.eclipse.jface.text.Region;
|
||||||
import org.eclipse.text.edits.TextEdit;
|
import org.eclipse.text.edits.TextEdit;
|
||||||
|
|
||||||
public class CCodeFormatter extends CodeFormatter {
|
public class CCodeFormatter extends CodeFormatter {
|
||||||
|
@ -108,10 +109,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 (Map.Entry<String, ?> entry : options.entrySet()) {
|
for (String key : options.keySet()) {
|
||||||
Object value = entry.getValue();
|
Object value= options.get(key);
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
formatterPrefs.put(entry.getKey(), (String) value);
|
formatterPrefs.put(key, (String) value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
preferences= new DefaultCodeFormatterOptions(formatterPrefs);
|
preferences= new DefaultCodeFormatterOptions(formatterPrefs);
|
||||||
|
@ -122,66 +123,10 @@ 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,
|
||||||
TextEdit edit= null;
|
String lineSeparator) {
|
||||||
if (lineSeparator != null) {
|
|
||||||
preferences.line_separator = lineSeparator;
|
|
||||||
} else {
|
|
||||||
preferences.line_separator = System.getProperty("line.separator"); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
preferences.initial_indentation_level = indentationLevel;
|
preferences.initial_indentation_level = indentationLevel;
|
||||||
|
return format(kind, source, new IRegion[] { new Region(offset, length) }, lineSeparator)[0];
|
||||||
ITranslationUnit tu = getTranslationUnit(source);
|
|
||||||
if (tu != null) {
|
|
||||||
IIndex index;
|
|
||||||
try {
|
|
||||||
index = CCorePlugin.getIndexManager().getIndex(tu.getCProject());
|
|
||||||
index.acquireReadLock();
|
|
||||||
} catch (CoreException e) {
|
|
||||||
throw new AbortFormatting(e);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
IASTTranslationUnit ast;
|
|
||||||
try {
|
|
||||||
try {
|
|
||||||
ast= tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
|
|
||||||
} catch (CoreException exc) {
|
|
||||||
throw new AbortFormatting(exc);
|
|
||||||
}
|
|
||||||
CodeFormatterVisitor codeFormatter = new CodeFormatterVisitor(preferences, offset, length);
|
|
||||||
edit= codeFormatter.format(source, ast);
|
|
||||||
IStatus status= codeFormatter.getStatus();
|
|
||||||
if (!status.isOK()) {
|
|
||||||
CCorePlugin.log(status);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
index.releaseReadLock();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
IncludeFileContentProvider contentProvider = IncludeFileContentProvider.getSavedFilesProvider();
|
|
||||||
IScannerInfo scanInfo = new ScannerInfo();
|
|
||||||
FileContent content = FileContent.create("<text>", source.toCharArray()); //$NON-NLS-1$
|
|
||||||
|
|
||||||
ILanguage language= (ILanguage) options.get(DefaultCodeFormatterConstants.FORMATTER_LANGUAGE);
|
|
||||||
if (language == null) {
|
|
||||||
language= GPPLanguage.getDefault();
|
|
||||||
}
|
|
||||||
IASTTranslationUnit ast;
|
|
||||||
try {
|
|
||||||
ast= language.getASTTranslationUnit(content, scanInfo, contentProvider, null, 0,
|
|
||||||
ParserUtil.getParserLogService());
|
|
||||||
CodeFormatterVisitor codeFormatter = new CodeFormatterVisitor(preferences, offset, length);
|
|
||||||
edit= codeFormatter.format(source, ast);
|
|
||||||
IStatus status= codeFormatter.getStatus();
|
|
||||||
if (!status.isOK()) {
|
|
||||||
CCorePlugin.log(status);
|
|
||||||
}
|
|
||||||
} catch (CoreException e) {
|
|
||||||
throw new AbortFormatting(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return edit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Add table
Reference in a new issue