/** * @param element * @param iBuildpathEntry * @return the name of the container description */ private String getEntryDescription(Object element, IBuildpathEntry iBuildpathEntry) { IProject project = ((IncludePath) element).getProject(); IScriptProject scriptProject = DLTKCore.create(project); IBuildpathContainer buildpathContainer = null; try { buildpathContainer = DLTKCore.getBuildpathContainer(iBuildpathEntry.getPath(), scriptProject); } catch (ModelException e) { // no matching container - return the path } if (buildpathContainer != null) { return buildpathContainer.getDescription(); } return iBuildpathEntry.getPath().toOSString(); }
private void removeBuildPath(IResource resource, IProject project) { IScriptProject projrct = DLTKCore.create(project); IPath filePath = resource.getFullPath(); oldBuildEntries = Arrays.asList(projrct.readRawBuildpath()); newBuildEntries = new ArrayList<IBuildpathEntry>(); newBuildEntries.addAll(oldBuildEntries); for (int i = 0; i < oldBuildEntries.size(); i++) { IBuildpathEntry fEntryToChange = oldBuildEntries.get(i); IPath entryPath = fEntryToChange.getPath(); int mattchedPath = entryPath.matchingFirstSegments(filePath); if (mattchedPath == filePath.segmentCount()) { newBuildEntries.remove(fEntryToChange); } else { IBuildpathEntry newEntry = RefactoringUtility.createNewBuildpathEntry( fEntryToChange, fEntryToChange.getPath(), filePath, ""); // $NON-NLS-1$ newBuildEntries.remove(fEntryToChange); newBuildEntries.add(newEntry); } } oldIncludePath = new ArrayList<IBuildpathEntry>(); newIncludePathEntries = new ArrayList<IBuildpathEntry>(); List<IncludePath> includePathEntries = Arrays.asList(IncludePathManager.getInstance().getIncludePaths(project)); for (IncludePath entry : includePathEntries) { Object includePathEntry = entry.getEntry(); IResource includeResource = null; if (!(includePathEntry instanceof IBuildpathEntry)) { includeResource = (IResource) includePathEntry; IPath entryPath = includeResource.getFullPath(); IBuildpathEntry oldEntry = RefactoringUtility.createNewBuildpathEntry(IBuildpathEntry.BPE_SOURCE, entryPath); oldIncludePath.add((IBuildpathEntry) oldEntry); if (filePath.isPrefixOf(entryPath) || entryPath.equals(filePath)) { } else { IBuildpathEntry newEntry = RefactoringUtility.createNewBuildpathEntry(IBuildpathEntry.BPE_SOURCE, entryPath); newIncludePathEntries.add(newEntry); } } else { newIncludePathEntries.add((IBuildpathEntry) includePathEntry); oldIncludePath.add((IBuildpathEntry) includePathEntry); } } }
private Map<IFile, Program> collectReferencingFiles(IFile sourceFile, IProgressMonitor pm) { ISourceModule sourceModule = DLTKCore.createSourceModuleFrom(sourceFile); Map<IFile, Program> participantFiles = new HashMap<IFile, Program>(); Collection<Node> references = MoveUtils.getReferencingFiles(sourceModule); if (references != null) { for (Iterator<Node> it = references.iterator(); it.hasNext(); ) { Node node = it.next(); IFile file = (IFile) node.getFile().getResource(); try { participantFiles.put(file, RefactoringUtility.getProgramForFile(file)); } catch (Exception e) { } } } return participantFiles; }
public void resourceChanged(IResourceChangeEvent event) { if (event.getBuildKind() == IncrementalProjectBuilder.CLEAN_BUILD) { Object source = event.getSource(); try { if (source instanceof IProject) { IProject project = (IProject) source; ProjectIndexerManager.removeProject(project.getFullPath()); ProjectIndexerManager.indexProject(project); } else if (source instanceof IWorkspace) { IWorkspace workspace = (IWorkspace) source; IProject[] projects = workspace.getRoot().getProjects(); // remove from index: for (IProject project : projects) { if (!project.isAccessible()) { continue; } IScriptProject scriptProject = DLTKCore.create(project); if (scriptProject.isOpen()) { IProjectFragment[] projectFragments = scriptProject.getProjectFragments(); for (IProjectFragment projectFragment : projectFragments) { ProjectIndexerManager.removeProjectFragment( scriptProject, projectFragment.getPath()); } ProjectIndexerManager.removeProject(project.getFullPath()); } } // add to index: for (IProject project : projects) { if (!project.isAccessible()) { continue; } ProjectIndexerManager.indexProject(project); } } } catch (CoreException e) { Logger.logException(e); } } }
/* * Do the actual modifications on the project */ private void modifyProject(IProject project) throws CoreException, ModelException { final PHPNature phpNature = new PHPNature(); // add the required builders and build paths as defined in the new PHP // nature phpNature.setProject(project); phpNature.configure(); IScriptProject scriptProject = DLTKCore.create(project); // merge the project build path with the old include path IBuildpathEntry[] existingPath = scriptProject.getRawBuildpath(); ArrayList<IBuildpathEntry> newPath = new ArrayList<IBuildpathEntry>(); if (existingPath != null) { newPath.addAll(Arrays.asList(existingPath)); } ProjectBackwardCompatibilityUtil unit = new ProjectBackwardCompatibilityUtil(); IBuildpathEntry[] oldIncludePath = unit.convertIncludePathForProject(project); if (oldIncludePath != null) { newPath.addAll(Arrays.asList(oldIncludePath)); } scriptProject.setRawBuildpath( newPath.toArray(new IBuildpathEntry[newPath.size()]), new NullProgressMonitor()); }
@Override public Image getImage(Object element) { IModelElement modelElement = null; if (element instanceof ExternalProjectFragment) { return PHPPluginImages.get(PHPPluginImages.IMG_OBJS_LIBRARY); } if (element instanceof IncludePath) { Object entry = ((IncludePath) element).getEntry(); // An included PHP project if (entry instanceof IBuildpathEntry) { int entryKind = ((IBuildpathEntry) entry).getEntryKind(); if (entryKind == IBuildpathEntry.BPE_PROJECT) { return PHPPluginImages.get(PHPPluginImages.IMG_OBJS_PHP_PROJECT); } // A library if (entryKind == IBuildpathEntry.BPE_LIBRARY || entryKind == IBuildpathEntry.BPE_CONTAINER) { return PHPPluginImages.get(PHPPluginImages.IMG_OBJS_LIBRARY); } } if (entry instanceof ExternalProjectFragment) { return PHPPluginImages.get(PHPPluginImages.IMG_OBJS_LIBRARY); } // Folder in the include path, should have same image as in the PHP // Explorer . if (entry instanceof IFolder) { IModelElement createdScriptFolder = DLTKCore.create((IFolder) entry); if (null == createdScriptFolder) return getImage(entry); return getImage(createdScriptFolder); } if (entry instanceof IResource) { return (getImage((IResource) entry)); } return null; } if (element instanceof IResource) { modelElement = DLTKCore.create((IResource) element); } else if (element instanceof IModelElement) { modelElement = (IModelElement) element; } if (modelElement != null) { IScriptProject project = modelElement.getScriptProject(); if (!project.isOnBuildpath(modelElement)) { // not in build path, // hence: hollow, // non-pakg icons if (modelElement.getElementType() == IModelElement.SOURCE_MODULE) return PHPPluginImages.get(PHPPluginImages.IMG_OBJS_CUNIT_RESOURCE); if (modelElement.getElementType() == IModelElement.PROJECT_FRAGMENT || modelElement.getElementType() == IModelElement.SCRIPT_FOLDER) return PHPPluginImages.get(PHPPluginImages.IMG_OBJS_PHP_FOLDER); } else { // in build path ... if (modelElement.getElementType() == IModelElement.SCRIPT_FOLDER || element instanceof IFolder) return PHPPluginImages.get(PHPPluginImages.IMG_OBJS_PHPFOLDER_ROOT); } } try { if (element instanceof IType && PHPFlags.isTrait(((IType) element).getFlags())) { return PHPPluginImages.get(PHPPluginImages.IMG_OBJS_TRAIT); } } catch (ModelException e) { } if (element != null) { for (ILabelProvider provider : TreeContentProviderRegistry.getInstance().getLabelProviders()) { Image image = provider.getImage(element); if (image != null) { return image; } } } return super.getImage(element); }
/** * Creates the text changes for all the affected files. Updates all the include statements in the * current file and all the includes in the "including " files. In case of folders, creates the * changes recursively * * @param pm - progress monitor * @param rootChange - the root change that the new changes are added to * @param sourceResources * @return the root change after the additions * @throws CoreException */ private Change createTextChanges( IProgressMonitor pm, CompositeChange rootChange, Set<IFile> phpFiles, IResource[] sourceResources) throws CoreException { List<ProgramFileChange> changes = new ArrayList<ProgramFileChange>(); try { pm.beginTask(PhpRefactoringCoreMessages.getString("MoveDelegate.1"), 100); // $NON-NLS-1$ // creat text changes: // for each file that will be moved, update its includes // and update all the files that include it, IResource[] uniqueSourceResources = removeDuplicateResources(sourceResources); for (Iterator<IFile> it = phpFiles.iterator(); it.hasNext(); ) { IFile currentMovedResource = it.next(); Map<IFile, Program> participantFiles = collectReferencingFiles(currentMovedResource, pm); for (Entry<IFile, Program> entry : participantFiles.entrySet()) { final IFile file = entry.getKey(); if (phpFiles.contains(file)) { continue; } final Program program = entry.getValue(); final ChangeIncludePath rename = new ChangeIncludePath( currentMovedResource, file, fMainDestinationPath, false, uniqueSourceResources); // aggregate the changes identifiers program.accept(rename); if (pm.isCanceled()) throw new OperationCanceledException(); pm.worked(1); if (rename.hasChanges()) { ProgramFileChange change = new ProgramFileChange(file.getName(), file, program); change.setEdit(new MultiTextEdit()); change.setTextType("php"); // $NON-NLS-1$ changes.add(change); rename.updateChange(change); } } ISourceModule sourceModule = DLTKCore.createSourceModuleFrom(currentMovedResource); if (sourceModule instanceof ISourceModule) { Program program = null; try { program = ASTUtils.createProgramFromSource(sourceModule); } catch (Exception e) { } if (program != null) { final ChangeIncludePath rename = new ChangeIncludePath( currentMovedResource, currentMovedResource, fMainDestinationPath, true, uniqueSourceResources); // aggregate the changes identifiers program.accept(rename); if (pm.isCanceled()) throw new OperationCanceledException(); pm.worked(1); if (rename.hasChanges()) { ProgramFileChange change = new ProgramFileChange( currentMovedResource.getName(), currentMovedResource, program); change.setEdit(new MultiTextEdit()); change.setTextType("php"); // $NON-NLS-1$ changes.add(change); rename.updateChange(change); } } } } pm.worked(70); } finally { pm.done(); } // getChildren() Map<IFile, List<TextEdit>> changeMap = new HashMap<IFile, List<TextEdit>>(); Map<IFile, ProgramFileChange> fileMap = new HashMap<IFile, ProgramFileChange>(); for (ProgramFileChange programFileChange : changes) { List<TextEdit> list = changeMap.get(programFileChange.getFile()); if (list == null) { list = new ArrayList<TextEdit>(); changeMap.put(programFileChange.getFile(), list); fileMap.put(programFileChange.getFile(), programFileChange); } else { } list.addAll(Arrays.asList(programFileChange.getEdit().getChildren())); } for (IFile file : changeMap.keySet()) { ProgramFileChange change = new ProgramFileChange(file.getName(), file, fileMap.get(file).getProgram()); change.setEdit(new MultiTextEdit()); change.setTextType("php"); // $NON-NLS-1$ List<TextEdit> list = changeMap.get(file); Collections.sort( list, new Comparator<TextEdit>() { public int compare(TextEdit o1, TextEdit o2) { return o2.getOffset() - o1.getOffset(); } }); for (TextEdit textEdit : list) { if (textEdit instanceof ReplaceEdit) { ReplaceEdit replaceEdit = (ReplaceEdit) textEdit; change.addEdit( new ReplaceEdit( replaceEdit.getOffset(), replaceEdit.getLength(), replaceEdit.getText())); } } rootChange.add(change); } return rootChange; }