Search in sources :

Example 31 with TextFileChange

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;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) Def_ModulePar_Template(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_ModulePar_Template) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) ArrayList(java.util.ArrayList) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Def_ModulePar(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_ModulePar) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) DeleteEdit(org.eclipse.text.edits.DeleteEdit) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) CoreException(org.eclipse.core.runtime.CoreException) Module(org.eclipse.titan.designer.AST.Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Location(org.eclipse.titan.designer.AST.Location)

Example 32 with TextFileChange

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;
}
Also used : IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) Hit(org.eclipse.titan.designer.AST.ReferenceFinder.Hit) Identifier(org.eclipse.titan.designer.AST.Identifier) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Module(org.eclipse.titan.designer.AST.Module) ASN1Module(org.eclipse.titan.designer.AST.ASN1.definitions.ASN1Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 33 with TextFileChange

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();
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) IProject(org.eclipse.core.resources.IProject) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) HashSet(java.util.HashSet)

Example 34 with TextFileChange

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;
}
Also used : TTCNPPEditor(org.eclipse.titan.designer.editors.ttcnppeditor.TTCNPPEditor) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TTCN3Editor(org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IEditorPart(org.eclipse.ui.IEditorPart) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) IProject(org.eclipse.core.resources.IProject)

Example 35 with TextFileChange

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;
}
Also used : TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) Module(org.eclipse.titan.designer.AST.Module) ImportModule(org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule) TTCN3Module(org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)37 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)18 TextChange (org.eclipse.ltk.core.refactoring.TextChange)12 IFile (org.eclipse.core.resources.IFile)11 CoreException (org.eclipse.core.runtime.CoreException)11 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)10 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)10 TextEdit (org.eclipse.text.edits.TextEdit)9 InsertEdit (org.eclipse.text.edits.InsertEdit)8 Module (org.eclipse.titan.designer.AST.Module)8 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)6 ArrayList (java.util.ArrayList)5 IDocument (org.eclipse.jface.text.IDocument)5 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)3 IProject (org.eclipse.core.resources.IProject)3