/** * @param scriptFile * @param position */ private FunctionStatement getFunctionStatement(final IFile scriptFile, final int position) { Script script = JavaScriptParserUtil.parse(DLTKCore.createSourceModuleFrom(scriptFile)); final ASTNode[] closestValue = new ASTNode[1]; ASTVisitor finder = new ASTVisitor() { @Override public boolean visitGeneral(ASTNode node) throws Exception { if (node.sourceStart() > position) return false; if (node.sourceEnd() >= position) { closestValue[0] = node; } return true; }; }; try { script.traverse(finder); } catch (Exception e) { e.printStackTrace(); } ASTNode node = closestValue[0]; while (node instanceof JSNode) { if (node instanceof FunctionStatement) { return (FunctionStatement) node; } node = ((JSNode) node).getParent(); } return null; }
@Test public void test(String fileName) { PdttFileExt testFile = filesMap.get(fileName); IFile file = project.findFile(testFile.getTestFiles().get(0).getName()); IStructuredModel model = null; try { model = createUnManagedStructuredModelFor(file); } catch (IOException e) { fail(e.getMessage()); } catch (CoreException e) { fail(e.getMessage()); } assertNotNull(model); IStructuredDocument structuredDocument = model.getStructuredDocument(); assertNotNull(structuredDocument); int start = Integer.valueOf(testFile.getConfig().get("start")); int length = Integer.valueOf(testFile.getConfig().get("length")); String visibility = testFile.getConfig().get("visibility"); ExtractFunctionRefactoring processor = new ExtractFunctionRefactoring( DLTKCore.createSourceModuleFrom(file), structuredDocument, start, length); if ("default".equals(visibility)) { processor.setVisibility(Modifiers.AccDefault); } if ("public".equals(visibility)) { processor.setVisibility(Modifiers.AccPublic); } if ("prvate".equals(visibility)) { processor.setVisibility(Modifiers.AccPrivate); } if ("protected".equals(visibility)) { processor.setVisibility(Modifiers.AccProtected); } processor.setNewFunctionName(testFile.getConfig().get("newName")); checkInitCondition(processor); performChange(processor); checkTestResult(testFile, structuredDocument); }
public String getDescription() { FunctionStatement f = getFunctionStatement(getScriptFile(), getProblemStartIdx()); ISourceModule sourceModule = DLTKCore.createSourceModuleFrom(getScriptFile()); try { String functionString = sourceModule.getBuffer().getText(f.sourceStart(), f.sourceEnd() - f.sourceStart()); int lineEnd = functionString.indexOf('\n'); StringBuilder sb = new StringBuilder(lineEnd + 100); sb.append("<html><body> *<b>"); sb.append(getAnnotation()); sb.append("</b><br/> */<br/>"); sb.append(functionString.substring(0, lineEnd)); sb.append("</body></html>"); return sb.toString(); } catch (ModelException e) { } return getLabel(); }
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 CompletionEngine_Test() { String filePath = ITestResourcesConstants.TR_CA + "/" + "testCodeCompletion.d"; IFile file = SampleMainProject.scriptProject.getProject().getFile(filePath); this.srcModule = DLTKCore.createSourceModuleFrom(file); }
protected IModelElement[] getSelection(String data) throws Exception { SourceRange range = createFile(data); ISourceModule sourceModule = DLTKCore.createSourceModuleFrom(testFile); IModelElement[] elements = sourceModule.codeSelect(range.getOffset(), range.getLength()); return elements; }
protected ISourceModule getSourceModule() { return DLTKCore.createSourceModuleFrom(testFile); }
/** * 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; }