Java Code Examples for org.eclipse.jdt.core.ICompilationUnit#isWorkingCopy()

The following examples show how to use org.eclipse.jdt.core.ICompilationUnit#isWorkingCopy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorkspaceEventsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public void didChangeWatchedFiles(DidChangeWatchedFilesParams param) {
	List<FileEvent> changes = param.getChanges().stream().distinct().collect(Collectors.toList());
	for (FileEvent fileEvent : changes) {
		CHANGE_TYPE changeType = toChangeType(fileEvent.getType());
		if (changeType == CHANGE_TYPE.DELETED) {
			cleanUpDiagnostics(fileEvent.getUri());
			handler.didClose(new DidCloseTextDocumentParams(new TextDocumentIdentifier(fileEvent.getUri())));
			discardWorkingCopies(fileEvent.getUri());
		}
		ICompilationUnit unit = JDTUtils.resolveCompilationUnit(fileEvent.getUri());
		if (unit != null && changeType == CHANGE_TYPE.CREATED && !unit.exists()) {
			final ICompilationUnit[] units = new ICompilationUnit[1];
			units[0] = unit;
			try {
				ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
					@Override
					public void run(IProgressMonitor monitor) throws CoreException {
						units[0] = createCompilationUnit(units[0]);
					}
				}, new NullProgressMonitor());
			} catch (CoreException e) {
				JavaLanguageServerPlugin.logException(e.getMessage(), e);
			}
			unit = units[0];
		}
		if (unit != null) {
			if (unit.isWorkingCopy()) {
				continue;
			}
			if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CHANGED) {
				if (unit.equals(CoreASTProvider.getInstance().getActiveJavaElement())) {
					CoreASTProvider.getInstance().disposeAST();
				}
			}
		}
		pm.fileChanged(fileEvent.getUri(), changeType);
	}
}
 
Example 2
Source File: ProblemsLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IAnnotationModel isInJavaAnnotationModel(ICompilationUnit original) {
	if (original.isWorkingCopy()) {
		FileEditorInput editorInput= new FileEditorInput((IFile) original.getResource());
		return JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getAnnotationModel(editorInput);
	}
	return null;
}
 
Example 3
Source File: TextSelectionConverter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException {
		if (input instanceof ICodeAssist) {
			if (input instanceof ICompilationUnit) {
				ICompilationUnit cunit= (ICompilationUnit)input;
				if (cunit.isWorkingCopy())
					JavaModelUtil.reconcile(cunit);
			}
			IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength());
			if (elements != null && elements.length > 0)
				return elements;
		}
		return EMPTY_RESULT;
}
 
Example 4
Source File: EditorUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean mustSaveDirtyEditor(IEditorPart ep, IEditorInput input, boolean saveUnknownEditors) {
	/*
	 * Goal: save all editors that could interfere with refactoring operations.
	 *
	 * Always save all editors for compilation units that are not working copies.
	 * (Leaving them dirty would cause problems, since the file buffer could have been
	 * modified but the Java model is not reconciled.)
	 *
	 * If <code>saveUnknownEditors</code> is <code>true</code>, save all editors
	 * whose implementation is probably not based on file buffers.
	 */
	IResource resource= (IResource) input.getAdapter(IResource.class);
	if (resource == null)
		return saveUnknownEditors;

	IJavaElement javaElement= JavaCore.create(resource);
	if (javaElement instanceof ICompilationUnit) {
		ICompilationUnit cu= (ICompilationUnit) javaElement;
		if (!cu.isWorkingCopy()) {
			return true;
		}
	}

	if (! (ep instanceof ITextEditor))
		return saveUnknownEditors;

	ITextEditor textEditor= (ITextEditor) ep;
	IDocumentProvider documentProvider= textEditor.getDocumentProvider();
	if (! (documentProvider instanceof TextFileDocumentProvider))
		return saveUnknownEditors;

	return false;
}
 
Example 5
Source File: MembersView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
boolean isInputAWorkingCopy() {
	Object input= getViewer().getInput();
	if (input instanceof IJavaElement) {
		ICompilationUnit cu= (ICompilationUnit)((IJavaElement)input).getAncestor(IJavaElement.COMPILATION_UNIT);
		if (cu != null)
			return cu.isWorkingCopy();
	}
	return false;
}
 
Example 6
Source File: CopyElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Copy/move the element from the source to destination, renaming
 * the elements as specified, honoring the collision policy.
 *
 * @exception JavaModelException if the operation is unable to
 * be completed
 */
protected void processElement(IJavaElement element) throws JavaModelException {
	JavaModelOperation op = getNestedOperation(element);
	boolean createElementInCUOperation =op instanceof CreateElementInCUOperation;
	if (op == null) {
		return;
	}
	if (createElementInCUOperation) {
		IJavaElement sibling = (IJavaElement) this.insertBeforeElements.get(element);
		if (sibling != null) {
			((CreateElementInCUOperation) op).setRelativePosition(sibling, CreateElementInCUOperation.INSERT_BEFORE);
		} else
			if (isRename()) {
				IJavaElement anchor = resolveRenameAnchor(element);
				if (anchor != null) {
					((CreateElementInCUOperation) op).setRelativePosition(anchor, CreateElementInCUOperation.INSERT_AFTER); // insert after so that the anchor is found before when deleted below
				}
			}
		String newName = getNewNameFor(element);
		if (newName != null) {
			((CreateElementInCUOperation) op).setAlteredName(newName);
		}
	}
	executeNestedOperation(op, 1);

	JavaElement destination = (JavaElement) getDestinationParent(element);
	ICompilationUnit unit= destination.getCompilationUnit();
	if (!unit.isWorkingCopy()) {
		unit.close();
	}

	if (createElementInCUOperation && isMove() && !isRenamingMainType(element, destination)) {
		JavaModelOperation deleteOp = new DeleteElementsOperation(new IJavaElement[] { element }, this.force);
		executeNestedOperation(deleteOp, 1);
	}
}
 
Example 7
Source File: DeleteElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes this element from its compilation unit.
 * @see MultiOperation
 */
protected void processElement(IJavaElement element) throws JavaModelException {
	ICompilationUnit cu = (ICompilationUnit) element;

	// keep track of the import statements - if all are removed, delete
	// the import container (and report it in the delta)
	int numberOfImports = cu.getImports().length;

	JavaElementDelta delta = new JavaElementDelta(cu);
	IJavaElement[] cuElements = ((IRegion) this.childrenToRemove.get(cu)).getElements();
	for (int i = 0, length = cuElements.length; i < length; i++) {
		IJavaElement e = cuElements[i];
		if (e.exists()) {
			deleteElement(e, cu);
			delta.removed(e);
			if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
				numberOfImports--;
				if (numberOfImports == 0) {
					delta.removed(cu.getImportContainer());
				}
			}
		}
	}
	if (delta.getAffectedChildren().length > 0) {
		cu.save(getSubProgressMonitor(1), this.force);
		if (!cu.isWorkingCopy()) { // if unit is working copy, then save will have already fired the delta
			addDelta(delta);
			setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
		}
	}
}
 
Example 8
Source File: CreateElementInCUOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Execute the operation - generate new source for the compilation unit
 * and save the results.
 *
 * @exception JavaModelException if the operation is unable to complete
 */
protected void executeOperation() throws JavaModelException {
	try {
		beginTask(getMainTaskName(), getMainAmountOfWork());
		JavaElementDelta delta = newJavaElementDelta();
		ICompilationUnit unit = getCompilationUnit();
		generateNewCompilationUnitAST(unit);
		if (this.creationOccurred) {
			//a change has really occurred
			unit.save(null, false);
			boolean isWorkingCopy = unit.isWorkingCopy();
			if (!isWorkingCopy)
				setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
			worked(1);
			this.resultElements = generateResultHandles();
			if (!isWorkingCopy // if unit is working copy, then save will have already fired the delta
					&& !Util.isExcluded(unit)
					&& unit.getParent().exists()) {
				for (int i = 0; i < this.resultElements.length; i++) {
					delta.added(this.resultElements[i]);
				}
				addDelta(delta);
			} // else unit is created outside classpath
			  // non-java resource delta will be notified by delta processor
		}
	} finally {
		done();
	}
}