public static String getPathFromInput(IEditorInput in) { IResource res = ResourceUtil.getResource(in); if (res != null) { try { if (res.getProject().hasNature(NATURE.ID)) { return res.getLocation().toOSString(); } } catch (CoreException ex) { Log.log(Log.LOG_WARNING, "Nature error", ex); //$NON-NLS-1$ } } if (in instanceof IURIEditorInput) { return Paths.get(((IURIEditorInput) in).getURI()).toString(); } else { return null; } }
/** * Gets the {@link IPath} from the given {@link IEditorInput}. * * @param editorInput * the {@link IEditorInput} * @return the {@link IPath} from the given {@link IEditorInput} if any, <code>null</code> otherwise */ public static IPath getPath(final IEditorInput editorInput) { final IPath path; if (editorInput instanceof ILocationProvider) { path = ((ILocationProvider)editorInput).getPath(editorInput); } else if (editorInput instanceof IURIEditorInput) { final URI uri = ((IURIEditorInput)editorInput).getURI(); if (uri != null) { final File osFile = URIUtil.toFile(uri); if (osFile != null) { path = Path.fromOSString(osFile.getAbsolutePath()); } else { path = null; } } else { path = null; } } else { path = null; } return path; }
@Override public boolean isModifiable(Object element) { if (isWorkspaceExternalEditorInput(element)) { URIInfo info= (URIInfo) getElementInfo(element); if (info != null) { if (info.updateCache) { try { updateCache((IURIEditorInput) element); } catch (CoreException x) { handleCoreException(x, "XtextDocumentProvider.isModifiable"); } } return info.isModifiable; } } return super.isModifiable(element); }
@Override public boolean isReadOnly(Object element) { if (isWorkspaceExternalEditorInput(element)) { URIInfo info= (URIInfo) getElementInfo(element); if (info != null) { if (info.updateCache) { try { updateCache((IURIEditorInput) element); } catch (CoreException x) { handleCoreException(x, "XtextDocumentProvider.isReadOnly"); } } return info.isReadOnly; } } return super.isReadOnly(element); }
/** * @since 2.3 */ protected void updateCache(IURIEditorInput input) throws CoreException { URIInfo info= (URIInfo) getElementInfo(input); if (info != null) { java.net.URI uri= input.getURI(); if (uri != null) { boolean readOnly = true; String uriAsString = uri.toString(); URI emfURI = URI.createURI(uriAsString); if (emfURI.isFile() && !emfURI.isArchive()) { // TODO: Should we use the resource set somehow to obtain the URIConverter for the file protocol? // see also todo below, but don't run into a stackoverflow ;-) Map<String, ?> attributes = URIConverter.INSTANCE.getAttributes(emfURI, null); readOnly = Boolean.TRUE.equals(attributes.get(URIConverter.ATTRIBUTE_READ_ONLY)); } info.isReadOnly= readOnly; info.isModifiable= !readOnly; } info.updateCache= false; } }
/** * Returns the URI for the specific editor input. * * @param input * the editor input * @return the URI, or null if none could be determined */ public static URI getURI(IEditorInput input) { if (input instanceof IFileEditorInput) { return ((IFileEditorInput) input).getFile().getLocationURI(); } if (input instanceof IURIEditorInput) { return ((IURIEditorInput) input).getURI(); } if (input instanceof IPathEditorInput) { return URIUtil.toURI(((IPathEditorInput) input).getPath()); } return null; }
private IPath getPath(IEditorPart otherEditor) { IEditorInput input = otherEditor.getEditorInput(); try { if (input instanceof IPathEditorInput) { return ((IPathEditorInput) input).getPath(); } URI uri = (URI) input.getAdapter(URI.class); if (uri != null) { return new Path(uri.getHost() + Path.SEPARATOR + uri.getPath()); } if (input instanceof IURIEditorInput) { return URIUtil.toPath(((IURIEditorInput) input).getURI()); } } catch (Exception e) { } return null; }
private static IPath getSourceFilePathFromEditorInput(IEditorInput editorInput) { if (editorInput instanceof IURIEditorInput) { URI uri = ((IURIEditorInput) editorInput).getURI(); if (uri != null) { IPath path = URIUtil.toPath(uri); if (path != null) { return path; } } } if (editorInput instanceof IFileEditorInput) { IFile file = ((IFileEditorInput) editorInput).getFile(); if (file != null) { return file.getLocation(); } } if (editorInput instanceof ILocationProvider) { return ((ILocationProvider) editorInput).getPath(editorInput); } return null; }
@Override public void partActivated(IWorkbenchPartReference partRef) { IEditorPart part = partRef.getPage().getActiveEditor(); if (!(part instanceof AbstractTextEditor)) return; // log new active file IEditorInput input = part.getEditorInput(); if (input instanceof IURIEditorInput) { URI uri = ((IURIEditorInput)input).getURI(); if (uri != null && uri.getPath() != null) { String currentFile = uri.getPath(); long currentTime = System.currentTimeMillis() / 1000; if (!currentFile.equals(WakaTime.getDefault().lastFile) || WakaTime.getDefault().lastTime + WakaTime.FREQUENCY * 60 < currentTime) { WakaTime.sendHeartbeat(currentFile, WakaTime.getActiveProject(), false); WakaTime.getDefault().lastFile = currentFile; WakaTime.getDefault().lastTime = currentTime; } } } }
public IPath getInputPath( IEditorInput input ) { if ( input instanceof IURIEditorInput ) { //return new Path( ( (IURIEditorInput) input ).getURI( ).getPath( ) ); URI uri = ( (IURIEditorInput) input ).getURI( ); if(uri == null && input instanceof IFileEditorInput) return ((IFileEditorInput)input).getFile( ).getFullPath( ); IPath localPath = URIUtil.toPath( uri ); String host = uri.getHost( ); if ( host != null && localPath == null ) { return new Path( host + uri.getPath( ) ).makeUNC( true ); } return localPath; } if ( input instanceof IFileEditorInput ) { return ( (IFileEditorInput) input ).getFile( ).getLocation( ); } return null; }
/** * Returns the element contained in the EditorInput */ Object getElementOfInput(IEditorInput input) { if (input instanceof IFileEditorInput) { return ((IFileEditorInput) input).getFile(); } if (input instanceof IURIEditorInput) { IURIEditorInput iuriEditorInput = (IURIEditorInput) input; URI uri = iuriEditorInput.getURI(); return new File(uri); } if (input instanceof PydevZipFileEditorInput) { PydevZipFileEditorInput pydevZipFileEditorInput = (PydevZipFileEditorInput) input; try { IStorage storage = pydevZipFileEditorInput.getStorage(); if (storage instanceof PydevZipFileStorage) { PydevZipFileStorage pydevZipFileStorage = (PydevZipFileStorage) storage; return pydevZipFileStorage; } } catch (CoreException e) { Log.log(e); } } return null; }
/** * @return the IEditorInput if we're dealing with an external file (or null otherwise) */ public static IEditorInput getExternalFileEditorInput(ITextEditor editor) { IEditorInput input = editor.getEditorInput(); //only return not null if it's an external file (IFileEditorInput marks a workspace file, not external file) if (input instanceof IFileEditorInput) { return null; } if (input instanceof IPathEditorInput) { //PydevFileEditorInput would enter here return input; } try { if (input instanceof IURIEditorInput) { return input; } } catch (Throwable e) { //IURIEditorInput not added until eclipse 3.3 } //Note that IStorageEditorInput is not handled for external files (files from zip) return input; }
/** * Use only for non-project parsers * @param {@link IFileEditorInput} {@link IResource} or null * @return true if refresh was triggered successfully */ private boolean refreshParser(PgDbParser parser, IResource res, IProgressMonitor monitor) throws InterruptedException, IOException, CoreException { if (res instanceof IFile && res.getProject().hasNature(NATURE.ID)) { parser.getObjFromProjFile((IFile) res, monitor); return true; } IEditorInput in = getEditorInput(); if (in instanceof IURIEditorInput) { IURIEditorInput uri = (IURIEditorInput) in; IDocument document = getDocumentProvider().getDocument(getEditorInput()); InputStream stream = new ByteArrayInputStream(document.get().getBytes(StandardCharsets.UTF_8)); parser.fillRefsFromInputStream(stream, Paths.get(uri.getURI()).toString(), monitor); return true; } return false; }
private InputStream openStream(IEditorInput input) throws CoreException, IOException { if (input instanceof IStorageEditorInput) { final IStorage storage = ((IStorageEditorInput) input).getStorage(); return storage.getContents(); } if (input instanceof IURIEditorInput) { final URI uri = ((IURIEditorInput) input).getURI(); return uri.toURL().openStream(); } throw new IOException("Unsupported input type: " + input.getClass()); //$NON-NLS-1$ }
private IDocumentProvider createDocumentProvider(IEditorInput input2) { // System.out.println(input2.getClass().toString()); if (input2 instanceof IFileEditorInput) { return new NCLDocumentProvider(); } else if (input2 instanceof IURIEditorInput) { NCLTextDocumentProvider docProvider = new NCLTextDocumentProvider(); return docProvider; } else { return new TextFileDocumentProvider(); } }
public File getCurrentFile() { File currentFile = null; if (getEditorInput() instanceof IFileEditorInput) { currentFile = new File(((IFileEditorInput) getEditorInput()) .getFile().getLocationURI()); } else { currentFile = new File(((IURIEditorInput) this.getEditorInput()) .getURI()); } return currentFile; }
@Override protected boolean setDocumentContent(IDocument document, IEditorInput editorInput, String encoding) throws CoreException { boolean result; if (isWorkspaceExternalEditorInput(editorInput)) { java.net.URI uri= ((IURIEditorInput) editorInput).getURI(); try { InputStream contentStream = null; try { contentStream = uri.toURL().openStream(); setDocumentContent(document, contentStream, encoding); } finally { try { if (contentStream != null) contentStream.close(); } catch (IOException e1) { } } } catch (IOException ex) { String message= (ex.getMessage() != null ? ex.getMessage() : ""); //$NON-NLS-1$ IStatus s= new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, message, ex); throw new CoreException(s); } result = true; } else { result = super.setDocumentContent(document, editorInput, encoding); } if (result) setDocumentResource((XtextDocument) document, editorInput, encoding); return result; }
@Override protected IAnnotationModel createAnnotationModel(Object element) throws CoreException { if (element instanceof IFileEditorInput) { IFileEditorInput input = (IFileEditorInput) element; return new XtextResourceMarkerAnnotationModel(input.getFile(), issueResolutionProvider, issueUtil); } else if (element instanceof IURIEditorInput) { return new AnnotationModel(); } return super.createAnnotationModel(element); }
/** * Checks if a file is text or binary. * * <p> * This method will first check if the file extension is known to be for a * binary or a text-based file. If the file extension is unknown then it * will try to analyze the file content. * </p> * * @param input * an editor input of the file * @return <code>true</code> if the file is more likely to be text file than * binary file, <code>false</code> otherwise. */ public static boolean isTextFile(IURIEditorInput input) { if (input == null) return false; String fileName = input.getName(); if (fileName == null) return false; String extension = FilenameUtils.getExtension(fileName).toUpperCase(); if (KnownTextBasedFileExtensions.set().contains(extension)) return true; if (KnownBinaryFileExtensions.set().contains(extension)) return false; URI uri = input.getURI(); if (uri != null) { InputStream is = null; try { is = uri.toURL().openStream(); return TextFileDetector.isTextFile(is); } catch (IOException e) { // Problem reading the editor input - avoid overriding } finally { // Make sure the input stream is closed close(is); } } return false; }
@Override public IEditorDescriptor overrideDefaultEditor(IEditorInput editorInput, IContentType contentType, IEditorDescriptor editorDescriptor) { // Override with default text editor only if no association is found yet if (editorDescriptor == null && editorInput != null && editorInput instanceof IURIEditorInput && isOverrideEnabled() && TextFileDetector.isTextFile((IURIEditorInput) editorInput)) { return getTextEditorDescriptor(); } return editorDescriptor; }
@Test public void testEditorInputNullName() { IURIEditorInput mockedInput = createMock(IURIEditorInput.class); expect(mockedInput.getName()).andReturn(null); replay(mockedInput); assertEquals(false, TextFileDetector.isTextFile(mockedInput)); }
@Test public void testEditorInputNullURI() { IURIEditorInput mockedInput = createMock(IURIEditorInput.class); expect(mockedInput.getName()).andReturn(""); expect(mockedInput.getURI()).andReturn(null); replay(mockedInput); assertEquals(false, TextFileDetector.isTextFile(mockedInput)); }
private void _testDetectTextFileByEditorInput(String fileName, boolean expected) { IURIEditorInput mockedInput = createNiceMock(IURIEditorInput.class); expect(mockedInput.getName()).andReturn(fileName); expect(mockedInput.getURI()).andReturn(new File(fileName).toURI()); replay(mockedInput); assertEquals(expected, TextFileDetector.isTextFile(mockedInput)); }
private static File getFile( IEditorInput editorInput ) { File result = null; if( editorInput instanceof IPathEditorInput ) { IPathEditorInput pathEditorInput = ( IPathEditorInput )editorInput; result = pathEditorInput.getPath().toFile(); } else if( editorInput instanceof IURIEditorInput ) { IURIEditorInput uriEditorInput = ( IURIEditorInput )editorInput; result = URIUtil.toFile( uriEditorInput.getURI() ); } return result; }
@Test public void testExecuteWithUriEditorInput() throws IOException { File file = tempFolder.newFile( "foo.txt" ); IURIEditorInput editorInput = mockUriEditorInput( file ); executeHandler( editorInput ); assertThat( file ).doesNotExist(); }
private static void checkEditorInput( IEditorInput input ) { if( !( input instanceof IStorageEditorInput ) && !( input instanceof IPathEditorInput ) && !( input instanceof IURIEditorInput ) ) { throw new IllegalArgumentException( "Invalid input: " + input ); } }
protected URI getURI() { // Now try and resolve the value as a URI... IEditorInput input = getEditorInput(); if (input instanceof IURIEditorInput) { return ((IURIEditorInput) input).getURI(); } if (input instanceof IFileEditorInput) { IFile file = ((IFileEditorInput) input).getFile(); return file.getLocationURI(); } return null; }
/** * Gets the indexing associated with the editor. * * @param editor * @return */ public static Index getIndex(AbstractThemeableEditor editor) { // NOTE: Moved from CommonContentAssistProcessor if (editor != null) { IEditorInput editorInput = editor.getEditorInput(); if (editorInput instanceof IFileEditorInput) { IFileEditorInput fileEditorInput = (IFileEditorInput) editorInput; IFile file = fileEditorInput.getFile(); return getIndexManager().getIndex(file.getProject().getLocationURI()); } if (editorInput instanceof IURIEditorInput) { IURIEditorInput uriEditorInput = (IURIEditorInput) editorInput; // FIXME This file may be a child, we need to check to see if there's an index with a parent URI. return getIndexManager().getIndex(uriEditorInput.getURI()); } if (editorInput instanceof IPathEditorInput) { IPathEditorInput pathEditorInput = (IPathEditorInput) editorInput; // FIXME This file may be a child, we need to check to see if there's an index with a parent URI. return getIndexManager().getIndex(URIUtil.toURI(pathEditorInput.getPath())); } } return null; }
/** * Creates a fake compilation unit. * * @param editorInput the URI editor input * @return the fake compilation unit * @since 3.3 */ private ICompilationUnit createFakeCompiltationUnit(IURIEditorInput editorInput) { try { final URI uri= editorInput.getURI(); final IFileStore fileStore= EFS.getStore(uri); final IPath path= URIUtil.toPath(uri); String fileStoreName= fileStore.getName(); if (fileStoreName == null || path == null) return null; WorkingCopyOwner woc= new WorkingCopyOwner() { /* * @see org.eclipse.jdt.core.WorkingCopyOwner#createBuffer(org.eclipse.jdt.core.ICompilationUnit) * @since 3.2 */ @Override public IBuffer createBuffer(ICompilationUnit workingCopy) { return new DocumentAdapter(workingCopy, fileStore, path); } }; IClasspathEntry[] cpEntries= null; IJavaProject jp= findJavaProject(path); if (jp != null) cpEntries= jp.getResolvedClasspath(true); if (cpEntries == null || cpEntries.length == 0) cpEntries= new IClasspathEntry[] { JavaRuntime.getDefaultJREContainerEntry() }; final ICompilationUnit cu= woc.newWorkingCopy(fileStoreName, cpEntries, getProgressMonitor()); if (!isModifiable(editorInput)) JavaModelUtil.reconcile(cu); return cu; } catch (CoreException ex) { return null; } }
/** * Get the file path from a FileStoreEditorInput. Removes any "file:" * from the URI to the file path if it exists. * * @param fileInput * @return String */ public String getFilePath(IEditorInput fileInput) { final IFile file = (IFile)fileInput.getAdapter(IFile.class); if (file!=null) return file.getLocation().toOSString(); if (fileInput instanceof IURIEditorInput) { String path = ((IURIEditorInput)fileInput).getURI().toString(); if (path.startsWith("file:")) path = path.substring(5); path = path.replace("%20", " "); return path; } return null; }
@Override public void caretMoved(CaretEvent event) { IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); if (window == null) return; if (window.getPartService() == null) return; if (window.getPartService().getActivePart() == null) return; if (window.getPartService().getActivePart().getSite() == null) return; if (window.getPartService().getActivePart().getSite().getPage() == null) return; if (window.getPartService().getActivePart().getSite().getPage().getActiveEditor() == null) return; if (window.getPartService().getActivePart().getSite().getPage().getActiveEditor().getEditorInput() == null) return; // log file if one is opened by default IEditorInput input = window.getPartService().getActivePart().getSite().getPage().getActiveEditor().getEditorInput(); if (input instanceof IURIEditorInput) { URI uri = ((IURIEditorInput)input).getURI(); if (uri != null && uri.getPath() != null) { String currentFile = uri.getPath(); long currentTime = System.currentTimeMillis() / 1000; if (!currentFile.equals(WakaTime.getDefault().lastFile) || WakaTime.getDefault().lastTime + WakaTime.FREQUENCY * 60 < currentTime) { WakaTime.sendHeartbeat(currentFile, WakaTime.getActiveProject(), false); WakaTime.getDefault().lastFile = currentFile; WakaTime.getDefault().lastTime = currentTime; } } } }
@Override public void postExecuteSuccess(String commandId, Object returnValue) { if (commandId.equals("org.eclipse.ui.file.save")) { IWorkbench workbench = PlatformUI.getWorkbench(); if (workbench == null) return; IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); if (window == null) return; if (window.getPartService() == null) return; if (window.getPartService().getActivePart() == null) return; if (window.getPartService().getActivePart().getSite() == null) return; if (window.getPartService().getActivePart().getSite().getPage() == null) return; if (window.getPartService().getActivePart().getSite().getPage().getActiveEditor() == null) return; if (window.getPartService().getActivePart().getSite().getPage().getActiveEditor().getEditorInput() == null) return; // log file save event IEditorInput input = window.getPartService().getActivePart().getSite().getPage().getActiveEditor().getEditorInput(); if (input instanceof IURIEditorInput) { URI uri = ((IURIEditorInput)input).getURI(); if (uri != null && uri.getPath() != null) { String currentFile = uri.getPath(); long currentTime = System.currentTimeMillis() / 1000; // always log writes WakaTime.sendHeartbeat(currentFile, WakaTime.getActiveProject(), true); WakaTime.getDefault().lastFile = currentFile; WakaTime.getDefault().lastTime = currentTime; } } } }
public void init( IEditorSite site, IEditorInput input ) throws PartInitException { cachedSite = site; if ( instance != null ) { getSite( ).getWorkbenchWindow( ) .getPartService( ) .removePartListener( instance ); instance.dispose( ); } if ( input instanceof IFileEditorInput || input instanceof IURIEditorInput ) { instance = new IDEMultiPageReportEditor( ); } else { instance = new MultiPageReportEditor( ); } // must add property listener before init. instance.addPropertyListener( this ); instance.init( site, input ); getSite( ).getWorkbenchWindow( ) .getPartService( ) .addPartListener( this ); }
@Override public URI getURI(Object element) { if (element instanceof IURIEditorInput) { IURIEditorInput editorInput = (IURIEditorInput) element; return editorInput.getURI(); } return null; }
/** * Responsavel por computar os valores que aparecerao na lista de sugestoes. * Retorna uma lista de ICompletionProposal */ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { // get the active IFile IWorkbench wb = PlatformUI.getWorkbench(); IWorkbenchWindow win = wb.getActiveWorkbenchWindow(); IWorkbenchPage page = win.getActivePage(); IEditorPart editor = page.getActiveEditor(); List <CompletionProposal> propList = new ArrayList <CompletionProposal>(); boolean isFileEditor = true; try { if (editor.getEditorInput() instanceof IFileEditorInput) { currentFile = new File( ((IFileEditorInput) editor.getEditorInput()).getFile() .getLocationURI()); isFileEditor = false; } else { currentFile = new File( ((IURIEditorInput) editor.getEditorInput()).getURI()); } } catch (Exception e) { e.printStackTrace(); ICompletionProposal[] proposal = new ICompletionProposal[0]; return proposal; } IDocument doc = viewer.getDocument(); text = doc.get(); NCLSourceDocument nclDoc = null; if (isFileEditor) nclDoc = NCLSourceDocument .createNCLSourceDocumentFromIDocument(doc); else nclDoc = (NCLSourceDocument) doc; isAttributeValue = nclDoc.isAttributeValue(offset); isAttribute = nclDoc.isAttribute(offset); isEndTagName = nclDoc.isEndTagName(offset); Point selectedRange = viewer.getSelectedRange(); if (selectedRange.y > 0) { // TODO: } else { // System.out.println("Attributo = " + isAttribute); String qualifier = getQualifier(nclDoc, offset); if (isEndTagName) { computeEndTagName(nclDoc, qualifier, offset, propList); } else if (isAttributeValue) { computeAttributesValuesProposals(nclDoc, qualifier, offset, propList); } else if (!isAttribute) { computeTagsProposals(nclDoc, qualifier, offset, propList); } else if (!nclDoc.isTagname(offset)) computeAttributesProposals(nclDoc, qualifier, offset, propList); } ICompletionProposal[] proposals = new ICompletionProposal[propList .size()]; propList.toArray(proposals); return proposals; }
/** * @since 2.3 */ protected boolean isWorkspaceExternalEditorInput(Object element) { return element instanceof IURIEditorInput && !(element instanceof IFileEditorInput); }
@Test public void testNullSafety() throws IOException { assertEquals(false, TextFileDetector.isTextFile((IURIEditorInput) null)); assertEquals(false, TextFileDetector.isTextFile((String) null)); assertEquals(false, TextFileDetector.isTextFile((InputStream) null)); }
private static IURIEditorInput mockUriEditorInput( File file ) { IURIEditorInput editorInput = mock( IURIEditorInput.class ); when( editorInput.getURI() ).thenReturn( file.toURI() ); return editorInput; }
public Object execute(ExecutionEvent event) throws ExecutionException { if (event == null) { return Boolean.FALSE; } Object context = event.getApplicationContext(); if (!(context instanceof IEvaluationContext)) { return Boolean.FALSE; } IEvaluationContext evContext = (IEvaluationContext) event.getApplicationContext(); Object input = evContext.getVariable(ISources.SHOW_IN_INPUT); if (input instanceof IFileEditorInput) { IFileEditorInput fei = (IFileEditorInput) input; return URIUtil.open(fei.getFile().getLocationURI()); } if (input instanceof IURIEditorInput) { IURIEditorInput uriInput = (IURIEditorInput) input; return URIUtil.open(uriInput.getURI()); } boolean result = Boolean.TRUE; @SuppressWarnings("unchecked") List<Object> selectedFiles = (List<Object>) evContext.getDefaultVariable(); if (selectedFiles.isEmpty()) { return Boolean.FALSE; } for (Object selected : selectedFiles) { IResource resource = null; if (selected instanceof IAdaptable) { resource = (IResource) ((IAdaptable) selected).getAdapter(IResource.class); if (resource != null) { result = result && URIUtil.open(resource.getLocationURI()); } else { IFileStore fileStore = (IFileStore) ((IAdaptable) selected).getAdapter(IFileStore.class); try { if (fileStore != null && fileStore.toLocalFile(EFS.NONE, null) != null) { result = result && URIUtil.open(fileStore.toURI()); } } catch (CoreException e) { } } } } return result; }