I have a Visual Studio extension package where I am applying C++ syntax settings to custom file extensions. This is done in the Visual Studio's Text Editor options. Those files are plain text and I mean to have them behave as code files in the IDE (IntelliSense, find matching braces, etc...)
It's mostly working fine, but there is one problem. The C++ syntax context is not applied to whichever is the first file I open in a given Visual Studio session. I will launch Visual Studio, open one of our custom projects, and open one file. The IDE opens a document window and the file is opened, can be edited and saved, no problem in appearance. But the file behaves as a plain text and not a C++ source. Now, whenever I open a second file in the IDE, or any further file, the C++ settings do get applied successfully. I can close all document tabs, and open new ones, and all those tabs are fine. Even re-opening the first file in a new tab, or after re-loading the project or the solution, is fine. Only the first document opened in a Visual Studio session has the issue.
For the following segment, I will refer to the Microsoft documentation on using their standard editor: https ://msdn.microsoft.com/en-us/library/bb166504.aspx
To implement the OpenItem method with a standard editor
1.Call IVsRunningDocumentTable (RDT_EditLock) to determine whether the document data object file is already open.
2.If the file is already open, resurface the file by calling the IsDocumentOpen method, specifying a value of IDO_ActivateIfOpen for the grfIDO parameter.
If the file is open and the document is owned by a different project than the calling project, your project receives a warning that the editor being opened is from another project. The file window is then surfaced.
3.If the document is not open or not in the running document table, call the OpenStandardEditor method (OSE_ChooseBestStdEditor) to open a standard editor for the file.
When you call the method, the IDE performs the following tasks:
a.The IDE scans the Editors/{guidEditorType}/Extensions subkey in the registry to determine which editor can open the file and has the highest priority for doing this.
b.After the IDE has determined which editor can open the file, the IDE calls CreateEditorInstance. The editor's implementation of this method returns information that is required for the IDE to call CreateDocumentWindow and site the newly opened document.
c.Finally, the IDE loads the document by using the usual persistence interface, such as IVsPersistDocData2.
d.If the IDE has previously determined that the hierarchy or hierarchy item is available, the IDE calls GetItemContext method on the project to get a project-level context IServiceProvider pointer to pass back in with the CreateDocumentWindow method call.
4.Return an IServiceProvider pointer to the IDE when the IDE calls GetItemContext on your project if you want to let the editor get context from your project.
Performing this step lets the project offer additional services to the editor.
If the document view or document view object was successfully sited in a window frame, the object is initialized with its data by calling LoadDocData.
It definitely seems to me that I need to hit element (D) from the above instructions. I have debuged through my extension code, and I do see where my implementation of GetItemContext() comes into play. When I open most files, the code path does effectively go through this method, however it does not when I open the first file of a Visual Studio session.
Call stack from OpenStandardEditor
GetItemContext is invoked by the Microsoft assemblies and I do not know what is the condition that triggers whether it is called or not. I can only trace up to my call to the method OpenStandardEditor(), in FileDocumentManager.cs, then I don't know what happens beyond that. The above screenshot is the call stack when GetItemContext is successfully invoked, but when I'm opening the first file I'm totally in the dark as to what OpenStandardEditor is doing. I do know that in both cases, when the context is loaded and when it is not, the exact same parameter values are passed to OpenStandardEditor. So here's my code where this method is invoked, if that can be of some help:
My override of class DocumentManager:
private int Open(bool newFile, bool openWith, uint editorFlags, ref Guid editorType, string physicalView, ref Guid logicalView, IntPtr docDataExisting, out IVsWindowFrame windowFrame, WindowFrameShowAction windowFrameAction)
{
windowFrame = null;
if (this.Node == null || this.Node.ProjectMgr == null || this.Node.ProjectMgr.IsClosed)
{
return VSConstants.E_FAIL;
}
int returnValue = VSConstants.S_OK;
string caption = this.GetOwnerCaption();
string fullPath = this.GetFullPathForDocument();
// Make sure that the file is on disk before we open the editor and display message if not found
if (!((FileNode)this.Node).IsFileOnDisk(true))
{
// Inform clients that we have an invalid item (wrong icon)
this.Node.OnInvalidateItems(this.Node.Parent);
// Bail since we are not able to open the item
return VSConstants.E_FAIL;
}
IVsUIShellOpenDocument uiShellOpenDocument = this.Node.ProjectMgr.Site.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
IOleServiceProvider serviceProvider = this.Node.ProjectMgr.Site.GetService(typeof(IOleServiceProvider)) as IOleServiceProvider;
try
{
int result = VSConstants.E_FAIL;
if (openWith)
{
result = uiShellOpenDocument.OpenStandardEditor((uint)__VSOSEFLAGS.OSE_UseOpenWithDialog, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
}
else
{
__VSOSEFLAGS openFlags = 0;
if (newFile)
{
openFlags |= __VSOSEFLAGS.OSE_OpenAsNewFile;
}
//NOTE: we MUST pass the IVsProject in pVsUIHierarchy and the itemid
// of the node being opened, otherwise the debugger doesn't work.
if (editorType != Guid.Empty)
{
result = uiShellOpenDocument.OpenSpecificEditor(editorFlags, fullPath, ref editorType, physicalView, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
}
else
{
openFlags |= __VSOSEFLAGS.OSE_ChooseBestStdEditor;
// THIS IS THE CALL THAT I'M ALWAYS INVOKING. PARAMS ARE ALWAYS THE SAME, BUT ITEM CONTEXT IS NOT ACTIVATED FOR FIRST FILE OF A SESSION.
result = uiShellOpenDocument.OpenStandardEditor((uint)openFlags, fullPath, ref logicalView, caption, this.Node.ProjectMgr, this.Node.ID, docDataExisting, serviceProvider, out windowFrame);
}
}
if (result != VSConstants.S_OK && result != VSConstants.S_FALSE && result != VSConstants.OLE_E_PROMPTSAVECANCELLED)
{
ErrorHandler.ThrowOnFailure(result);
}
if (windowFrame != null)
{
object var;
if (newFile)
{
ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out var));
IVsPersistDocData persistDocData = (IVsPersistDocData)var;
ErrorHandler.ThrowOnFailure(persistDocData.SetUntitledDocPath(fullPath));
}
var = null;
ErrorHandler.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocCookie, out var));
this.Node.DocCookie = (uint)(int)var;
if (windowFrameAction == WindowFrameShowAction.Show)
{
ErrorHandler.ThrowOnFailure(windowFrame.Show());
}
else if (windowFrameAction == WindowFrameShowAction.ShowNoActivate)
{
ErrorHandler.ThrowOnFailure(windowFrame.ShowNoActivate());
}
else if (windowFrameAction == WindowFrameShowAction.Hide)
{
ErrorHandler.ThrowOnFailure(windowFrame.Hide());
}
}
}
catch (COMException e)
{
Trace.WriteLine("Exception e:" + e.Message);
returnValue = e.ErrorCode;
this.CloseWindowFrame(ref windowFrame);
}
return returnValue;
}
I have also tried an alternative. In the call stack where I perform DoDefaultAction on my FileNode (extends HierarchyNode), I normally call an instance of my DocumentManager.Open() directly. I have changed that to try OpenDocumentViaProject() instead. Now, the MSENV assembly turns out to call my GetItemContext, then goes out to my implementation of DocumentManager.Open I quoted above.
Call stack from OpenDocumentViaProject
Sounds promising... but no. Beyond the screenshot above, once I call OpenStandardEditor the exact same behavior happens. No project context is applied to the first document opened in a session, and the context is applied to every further file. The call to GetItemContext() that is done by OpenDocumentViaProject() does not seem to matter in the slightest. Only when OpenStandardEditor() also ends up calling GetItemContext() somewhere downstream does the project settings I want get applied.
I don't see where I would be doing something fundamentally wrong. It seems to me that I am following the Mimcrosoft instructions on opening standard editors. Would you have a clue as to how my GetItemContext implementation is not invoked when I'm opening the first file of a VS session? Thanks
I have an Eclipse project that contains several source files, with a bunch of different encodings: some files are UTF8, some others are ISO-8859-1, others more are windows-1252.
Moreover, there are files whose encoding is explicit (it can bee seen in each file Properties window) while that of others is Inherited from container.
I need to convert them to UTF8 - and I've already found I can use iconv for that - see my answer here for details -, but since they're more than one thousand, I can't convert them one by one: is there any programmatic way to get the encoding from the IDE or something similar?
I'm on Windows, I may do some shell scripting and / or write auxiliary software.
The charset setting of project file could be found in ${PROJECT_FOLDER}/.settings/org.eclipse.core.resources.prefs
Since there are so many files in your project. Create a simple Eclipse plugin could reduce the effort. Here are the steps:
Select Eclipse menu item New > Project... > Plug-in Project
Accept default values and goto last page, choose Hello, World template.
Open and edit plugin.xml, adding required plugin org.eclipse.core.resources in Dependencies page.
Edit SampleAction.java, refer the sample code below..
Create a Debug Configuration for execute Eclipse Application.
Start the eclipse and import your project.
Select menu item Sample Menu to invoke public void run(IAction action).
Sample code of Eclipse resource API:
//import org.eclipse.core.resources.*;
//import org.eclipse.core.runtime.*;
public void run(IAction action) {
final boolean keepHistory = true;
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("yourProjectName");
project.accept(new IResourceVisitor() {
public boolean visit(IResource resource) {
IFile file = (IFile)resource;
try {
String charset = file.getCharset(true);
if (!"UTF-8".equals(charset)) {
InputStream is = file.getContents();
//convert to UTF-8 and save it
String str = new String(IOUtils.toByteArray(is), charset);
file.setContents(new ByteArrayInputStream(str.getBytes("UTF-8")), true, keepHistory, null);
//remove charset setting
file.setCharset("UTF-8", null);
}
} catch (Throwable e) {
//log error... maybe process later via project.getFile(new Path(projectRelativePath))
Activator.getDefault().getLog().log(new Status(IStatus.INFO, Activator.PLUGIN_ID,
file.getProjectRelativePath().toString(), e));
}
return false;
}
}, IResource.DEPTH_INFINITE, IResource.FILE);
}
I have created a plugin for a new language and used DLTK for indexing and searching feature.
I am using Eclipse Luna (PDE 3.10.1) and DLTK (5.0)
My question is:
How can I manually re-index a file and refresh the editor when I switch between tabs?
Because what happens now is if a file is reopened that time it gets re-indexed and error markers are updated, but while switching it doesn't update the error markers as dependent files are changed in other tabs.
I tried as below: It's indexing but not refreshing the editor.
I added a IPartListener2 and in partBroughtToTop() method i have following code for indexing and refreshing.
IModelElement model = EditorUtility.getEditorInputModelElement(partRef.getPage().getActiveEditor(), true);
if (model instanceof ISourceModule) {
ProblemCollector prob = new ProblemCollector();
SourceParserUtil.clearCache();
// get cache entry
final ISourceModuleInfo cacheEntry = ModelManager.getModelManager().getSourceModuleInfoCache().get((ISourceModule)model);
ModuleDeclaration mod = (ModuleDeclaration)SourceParserUtil.parse((ISourceModule)model, prob);
SourceParserUtil.putModuleToCache(cacheEntry, mod, prob);
SourceParserUtil.enableCache();
IEditorPart editor = partRef.getPage().getActiveEditor();
IEditorInput input = editor.getEditorInput();
try {
((ScriptEditor)editor).getDocumentProvider().resetDocument(input);
}
catch (CoreException e) {
}
}
Thanks in advance.
If I understand correctly, the issue is about re-validating files after changing dependencies.
1. It is not related to indexer (it just records that a file contains some elements)
2. It is not related to parser (which produces AST).
It should happen in a builder. You could try DLTK support for that by implementing IBuildParticipant or IScriptBuilder.
I am developing a eclipse plugin to ease the development using a proprietary version control system.
Right now there is only a command prompt version of the system available for this VCS and its run in terminal. So from my eclipse plugin I want to provide a simple menu options to do the things like check-out and check-in and internally call these commands.
But to run these commands I need to pass the argument 'path' of the selected .java file in the editor/project explorer. How can I get the path of the source file to the plugin?
Get the current workbench page:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
the workbench page implements ISelectionService so you can get the current selection:
ISelection selection = page.getSelection();
this will generally by an IStructuredSelection (but you need to check)
IStructuredSelection sel = (IStructuredSelection)selection;
See if this adapts to an IFile:
Object selObject = sel.getFirstElement(); // or iterate through all the selection elements
IFile file = Platform.getAdapterManager().getAdapter(selObject, IFile.class);
if you get a file the full path is:
String location = file.getLocation().toOSString();
If the current part is an editor then the selection you receive might be a text string. So you need to deal with editors separately:
IWorkbenchPart activePart = page.getActivePart();
if (activePart instanceof IEditorPart)
{
IEditorInput input = ((IEditorPart)activePart).getEditorInput();
if (input instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput)input).getFile();
...
}
}
I am writing Eclipse plugins for Java, and have the following problem:
Given an IEditorPart, I need to check if it is a java editor.
I could do (IEditor instanceof JavaEditor),
but JavaEditor is an org.eclipse.jdt.internal.ui.javaeditor.JavaEditor,
which falls under the JDT's "internal" classes.
Is there a smarter and safer way to do this? I'm not sure why there is no non-internal interface for this.
You should test the id of the IEditorPart:
private boolean isJavaEditor(IWorkbenchPartReference ref) {
if (ref == null) {
return false; }
String JavaDoc id= ref.getId();
return JavaUI.ID_CF_EDITOR.equals(id) || JavaUI.ID_CU_EDITOR.equals(id);
}
Testing the instance was only needed in eclipse3.1.
alt text http://blogs.zdnet.com/images/Burnette_DSCN0599.JPG
JavaUI is the main access point to the Java user interface components. It allows you to programmatically open editors on Java elements, open a Java or Java Browsing perspective, and open package and type prompter dialogs.
JavaUI is the central access point for the Java UI plug-in (id "org.eclipse.jdt.ui")
You can see that kind of utility function ("isJavaEditor()") used for instance in ASTProvider.
The mechanism of identification here is indeed simple String comparison.
Anyway, you are wise to avoid cast comparison with internal class: it has been listed as one of the 10 common errors in plugins development ;) .
One strategy might be to use JavaUI.getEditorInputJavaElement(IEditorPart):
// given IEditorPart editor
IJavaElement elt = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
if (elt != null) {
// editor is a Java editor
}
The method returns null if the editor input is not in fact a Java element.