use of org.eclipse.ltk.core.refactoring.TextFileChange in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method createFileChange.
private Change createFileChange(final IFile toVisit) {
if (toVisit == null) {
return null;
}
final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
final Module module = sourceParser.containedModule(toVisit);
if (module == null) {
return null;
}
// find all locations in the module that should be edited
final DefinitionVisitor vis = new DefinitionVisitor();
module.accept(vis);
final NavigableSet<Definition> nodes = vis.getLocations();
if (nodes.isEmpty()) {
return null;
}
// calculate edit locations
final List<Definition> locations = new ArrayList<Definition>();
try {
final WorkspaceJob job1 = calculateEditLocations(nodes, toVisit, locations);
job1.join();
} catch (InterruptedException ie) {
ErrorReporter.logExceptionStackTrace(ie);
} catch (CoreException ce) {
ErrorReporter.logError("UngroupModuleparRefactoring/CreateChange.createFileChange(): " + "CoreException while calculating edit locations. ");
ErrorReporter.logExceptionStackTrace(ce);
}
if (locations.isEmpty()) {
return null;
}
// create a change for each edit location
final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
final MultiTextEdit rootEdit = new MultiTextEdit();
tfc.setEdit(rootEdit);
int precedeOffset = -1;
final String fileContents = loadFileContent(toVisit);
for (Definition node : locations) {
final Location l = node.getCumulativeDefinitionLocation();
final Location typeLocation = node.getType(CompilationTimeStamp.getBaseTimestamp()).getLocation();
final Location identifierLocation = node.getIdentifier().getLocation();
if (precedeOffset != l.getOffset()) {
precedeOffset = l.getOffset();
final int len = l.getEndOffset() - l.getOffset();
rootEdit.addChild(new DeleteEdit(l.getOffset(), len + 1));
}
String typeText = fileContents.substring(typeLocation.getOffset(), typeLocation.getEndOffset()).trim();
String name = fileContents.substring(identifierLocation.getOffset(), identifierLocation.getEndOffset()).trim();
String newModulePar = "";
if (node instanceof Def_ModulePar) {
Def_ModulePar modulePar = (Def_ModulePar) node;
if (modulePar.getDefaultValue() != null) {
final Location valueLocation = modulePar.getDefaultValue().getLocation();
String valueText = fileContents.substring(valueLocation.getOffset(), valueLocation.getEndOffset()).trim();
newModulePar = "modulepar " + typeText + " " + name + " := " + valueText + ";\n";
} else {
newModulePar = "modulepar " + typeText + " " + name + ";\n";
}
} else if (node instanceof Def_ModulePar_Template) {
Def_ModulePar_Template modulePar = (Def_ModulePar_Template) node;
if (modulePar.getDefaultTemplate() != null) {
final Location valueLocation = modulePar.getDefaultTemplate().getLocation();
String temlateText = fileContents.substring(valueLocation.getOffset(), valueLocation.getEndOffset()).trim();
newModulePar = "modulepar template " + typeText + " " + name + " := " + temlateText + ";\n";
} else {
newModulePar = "modulepar template " + typeText + " " + name + ";\n";
}
}
rootEdit.addChild(new InsertEdit(l.getOffset(), newModulePar));
}
return tfc;
}
use of org.eclipse.ltk.core.refactoring.TextFileChange in project titan.EclipsePlug-ins by eclipse.
the class RenameRefactoring method createChange.
@Override
public Change createChange(final IProgressMonitor pm) throws CoreException {
CompositeChange result = new CompositeChange(getName());
// add the change of all found identifiers grouped by module
boolean isAsnRename = module.getModuletype() == Module.module_type.ASN_MODULE;
String newTtcnIdentifierName = newIdentifierName;
if (isAsnRename) {
newTtcnIdentifierName = Identifier.getTtcnNameFromAsnName(newIdentifierName);
}
List<IFile> filesToProcess = new ArrayList<IFile>(idsMap.size());
for (Module m : idsMap.keySet()) {
List<Hit> hitList = idsMap.get(m);
boolean isTtcnModule = m.getModuletype() == Module.module_type.TTCN3_MODULE;
IFile file = (IFile) hitList.get(0).identifier.getLocation().getFile();
TextFileChange tfc = new TextFileChange(file.getName(), file);
result.add(tfc);
MultiTextEdit rootEdit = new MultiTextEdit();
tfc.setEdit(rootEdit);
for (Hit hit : hitList) {
int offset = hit.identifier.getLocation().getOffset();
int length = hit.identifier.getLocation().getEndOffset() - offset;
String newName = isTtcnModule ? newTtcnIdentifierName : newIdentifierName;
// reference.
if (rf.fieldId == null && hit.reference != null && hit.reference.getModuleIdentifier() == null && rf.assignment.getMyScope().getModuleScope() != hit.reference.getMyScope().getModuleScope() && hit.reference.getMyScope().hasAssignmentWithId(CompilationTimeStamp.getBaseTimestamp(), new Identifier(isTtcnModule ? Identifier_type.ID_TTCN : Identifier_type.ID_ASN, newIdentifierName))) {
newName = rf.assignment.getMyScope().getModuleScope().getName() + "." + newName;
}
rootEdit.addChild(new ReplaceEdit(offset, length, newName));
}
filesToProcess.add((IFile) m.getLocation().getFile());
}
return result;
}
use of org.eclipse.ltk.core.refactoring.TextFileChange in project titan.EclipsePlug-ins by eclipse.
the class OrganizeImportsOp method run.
@Override
public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
final Set<IProject> projects = new HashSet<IProject>();
for (final IFile f : files) {
projects.add(f.getProject());
}
monitor.beginTask("Organize imports", files.size() + projects.size() * 20);
for (final IProject project : projects) {
final ProjectSourceParser parser = GlobalParser.getProjectSourceParser(project);
parser.reportOutdating(files);
final WorkspaceJob job = parser.analyzeAll();
monitor.subTask("Waiting for semantic analysis on project " + project.getName());
try {
job.join();
} catch (InterruptedException e) {
ErrorReporter.logExceptionStackTrace("Error while waiting for the analysis to finish", e);
}
if (monitor.isCanceled()) {
throw new InterruptedException();
}
monitor.worked(20);
}
final CompositeChange compChange = new CompositeChange("Organize imports");
for (final IFile f : files) {
monitor.subTask("Organizing " + f.getName());
try {
final TextFileChange change = OrganizeImports.organizeImportsChange(f);
compChange.add(change);
compChange.perform(new SubProgressMonitor(monitor, 1));
final MultiTextEdit edit = (MultiTextEdit) change.getEdit();
if (edit != null && edit.getChildrenSize() > 0) {
final WorkspaceJob job = GlobalParser.getProjectSourceParser(f.getProject()).reportOutdating(f);
try {
job.join();
} catch (InterruptedException e) {
ErrorReporter.logExceptionStackTrace("Error while waiting for the outdating report to finish", e);
}
}
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace("Error while organizing file " + f.getName(), e);
}
if (monitor.isCanceled()) {
throw new InterruptedException();
}
monitor.worked(1);
}
for (final IProject project : projects) {
GlobalParser.getProjectSourceParser(project).analyzeAll();
}
monitor.done();
}
use of org.eclipse.ltk.core.refactoring.TextFileChange in project titan.EclipsePlug-ins by eclipse.
the class OrganizeFromEditor method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editor == null || !(editor instanceof TTCN3Editor || editor instanceof TTCNPPEditor)) {
ErrorReporter.logError("The editor is not found or not a Titan TTCN-3 editor");
return null;
}
final IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
TextFileChange change = null;
try {
change = OrganizeImports.organizeImportsChange(file);
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace("Error while creating the needed import changes", e);
return null;
}
try {
change.perform(new NullProgressMonitor());
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace("Error while performing the needed import changes", e);
}
final IProject project = file.getProject();
GlobalParser.getProjectSourceParser(project).reportOutdating(file);
GlobalParser.getProjectSourceParser(project).analyzeAll();
return null;
}
use of org.eclipse.ltk.core.refactoring.TextFileChange in project titan.EclipsePlug-ins by eclipse.
the class ImportSelectionDialog method organizeImportsChange.
/**
* Organize the import statements of a file. The necessary changes are
* collected and returned in a <code>TextFileChange</code> object.
*
* @param file
* The file to organize.
* @return The change to perform.
* @throws CoreException
* when document associated with the file can't be acquired.
*/
public static TextFileChange organizeImportsChange(final IFile file) throws CoreException {
sortImports = Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, PreferenceConstants.ORG_IMPORT_SORT, true, null);
addImports = Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, PreferenceConstants.ORG_IMPORT_ADD, true, null);
removeImports = Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, PreferenceConstants.ORG_IMPORT_REMOVE, true, null);
importChangeMethod = Platform.getPreferencesService().getString(Activator.PLUGIN_ID, PreferenceConstants.ORG_IMPORT_METHOD, OrganizeImportPreferencePage.JUST_CHANGE, null);
final String designerId = ProductConstants.PRODUCT_ID_DESIGNER;
final String displayDebugInfo = org.eclipse.titan.designer.preferences.PreferenceConstants.DISPLAYDEBUGINFORMATION;
reportDebug = Platform.getPreferencesService().getBoolean(designerId, displayDebugInfo, false, null);
final TextFileChange change = new TextFileChange(file.getName(), file);
final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
final Module actualModule = projectSourceParser.containedModule(file);
if (!(actualModule instanceof TTCN3Module)) {
ErrorReporter.logError("The module is not a TTCN-3 module");
return change;
}
final TTCN3Module module = (TTCN3Module) actualModule;
final IDocument doc = change.getCurrentDocument(null);
try {
change.setEdit(organizeImportsEdit(module, doc));
} catch (BadLocationException e) {
ErrorReporter.logExceptionStackTrace("Error while organizing imports", e);
}
return change;
}
Aggregations