How to notify the editor about document changes - visual-studio-2012

I work on a custom project system and I have an issue with renaming items. The project system implementation is based on MPF and renaming items via the solution explorer worked more or less without any problems, but...
When an item gets renamed, I also update information which are stored in the file itself. In case the document is opened by the code editor, the text in the editor doesn´t get refreshed (only the document window´s caption changes to the new filename). If I save the open document all changes applied by the rename operation are overriden, of course.
How can I force the editor to reload the document, so that the automatic changes will be shown?

Assuming it's a text file versus some custom designer, use the IVsRunningDocumentTable interfaces to get the text buffer for the file that's currently open. The fourth iteration of the interface is the easiest one to use from managed code. You can call IsMonikerValid (where the "moniker" is the file name) to see if the file is open, and if so then call GetDocumentData to get the IVsTextBuffer for the file. That type itself is fairly annoying to work with, so if you're only supporting Visual Studio 2010 or later, then pass that to this function to get the newer editor APIs version of it.
As far as they "why" it's a good idea to do this: if you edit the file on disk and then try to force a reload, there are various problems you might run into. If the file wasn't saved before the rename, you might accidentally lose those unsaved edits. The reload might cause the undo history of the file to be lost, and any other extensions/features that were tracking points in the file with editor tracking spans or markers might lose the points they're tracking. Performance should be a bit better too if the file is a large file.

Related

Undo changes in Paint/image editor

Assuming that paint lets its users undo changes, I wonder whether it's possible to redo those changes by any computer means after the initial file has been saved.
Steps to reproduce:
1. Open a .jpg file in paint
2. Draw a line
3. Save as New.jpg
4. Open New.jpg in paint
5. Delete that line
Is the 5th step actually doable using any software/by coding whatever?
This is a question for SuperUser more than StackOverflow.
However, the answer is that it's only possible if the software stores the history of change steps to the saved file. Most common interchangeable image formats, like the JPG example in your question, do not support storage of that information.
An image editor could save to a format, likely to be specific to the editor, which preserves the history of alterations to the image. However, this could lead to a lot of storage of data that is usually not useful in the future, so there isn't a huge amount of demand for the feature.
Note that what is sought in the question could be done by a new operation to erase the line or parts of it, but not as an undo operation from the prior editing session.

How do you make a specific folder that has subfolders to be uneditable in Sublime Text

How do you make a specific folder that has subfolders to be uneditable in Sublime text? ? I know it's possible but how ? Like i have some old projects that I want to use as a reference to study my old code, but i"m worried that I mistakenly edited some parts of that specific module / file, when I'm mindlessly touring around my code.. How do I do it ? like making a specific folder to be uneditable in sublime text that only modifying it can change it . I mean I tried installing this one package : https://packagecontrol.io/packages/Toggle%20Read-Only
But it still gives me a prompt whenever I want to changed something from a file
Your best bet is to make sure that your source code is covered by some sort of Source Control, such as git or Subversion; this is just always a good idea in general and unrelated to your particular question. Having your code under source control means that when you edit a file (accidentally or on purpose) you can see exactly what you edited or throw those edits away and go back to what you had if you want to. If you also push your code to an external server, such as GitHUb (if you use git) then you also have a cheap and easy off-site backup of your code as well.
That said, if you want to make files uneditable, that's more the job of your file system than the tools that you're using to edit the files (in this case Sublime Text).
All file systems and operating systems should have the concept of a read-only file, which sounds like what you want. A file being marked as read-only stops you from accidentally modifying it; depending on the software that you use, edits are either impossible or will need to be confirmed.
In your case, you can do this in a couple of different ways. If you only want to protect a couple of files, then you would do a Right click and choose Properties; in the bottom of the General tab there is a check box you can check to make that file read-only:
If you have many files, you can do the same thing by editing the properties of the folder that contains the code instead:
When you do this to a folder, the property set works a little differently; since you're modifying the folder as a whole, you need to click the box twice to change it from a square (shown above) to a check mark. When you apply the change, you will be asked if you only want to make files inside of that directory read-only, or all files in that folder as well as all folders under it; choose as appropriate.
Sublime will let you open read-only files and will also let you modify their contents, but when you try to save you will get a warning dialog telling you that the file is write-protected; you need to confirm if you want to actually save changes or not; (other software may not prompt with such a dialog and may just fail):
If you choose to save, you will remove the read-only attribute and make the file normal again.
If you want to make a file completely un-editable so that you can't even accidentally change the file, you can achieve that with a simple plugin in combination with making the file read-only (see this video if you're not sure how to apply a plugin):
import sublime
import sublime_plugin
import os
class ReadOnlyListener(sublime_plugin.EventListener):
def on_load(self, view):
if (os.path.exists(view.file_name())
and not os.access(view.file_name(), os.W_OK)):
view.set_read_only(True)
EDIT: The internal View Package Files command will open package resources from sublime-package files transparently and give them a filename that represents where they would exist on disk if they were not in the package file.
The plugin from the original answer would stop you from being able to use this command by noting that the file is not writable (because it does not exist on disk) and make the view read-only, which stops the file content from being displayed because the view can't be modified.
This is rectified in the edit above by only taking action if the file actually exists on disk (the View Package File command already makes files it loads in this manner read-only if they do not exist on disk).
This makes an event listener that checks every time you open a file to see if the file is writable, and if it's not it makes the view read-only. This distinction is Sublime specific; regardless of the underlying state of the file, you just can't make any changes to such a file at all. That doesn't stop you from saving the file even if you haven't made any changes, which would have the same effect as the above.

Open Excel File from Command Line Unprotected View

I frequently open large excel files (100 MB+) from a network location that I completely trust. However, the default trusted domains on the workstation that I use does not include files on the network drive. It is cumbersome to load each excel file twice (once to open it from explorer, then again to 'enable editing', i.e. remove protected view).
Is there an argument/switch that can be provided to excel.exe from the command line that will force a file to be opened in unprotected(editable) view? I would find this less cumbersome than waiting for a large file to load twice.
I typically use Excel 2010 and 2013.
Note: I already know how to disable protected view through the excel interface, this is not the solution I am looking for, as these settings reset to their defaults everytime I log off the workstation.
I am also prepared to accept the fact that this might not be possible to do except in the Excel GUI. But if anyone has a workable solution, that would be brilliant. Thank you!
Is there an argument/switch that can be provided to excel.exe from the command line that will force a file to be opened in unprotected(editable) view?
No.
The protected view comes as a result of the level in the security settings, thus it would be a major security problem, if it can be avoided.
As a possible workaround - copy the files to your computer, mark it as trusted and open them there.

Matlab: open files 'outside Matlab' by default

I'm looking for a way to have Excel files in my Matlab folder open 'outside Matlab' (i.e., by MS Excel in most cases) directly by double-clicking the file, rather than right-clicking and selecting 'Open Outside Matlab'.
The .xls files reader built in Matlab can be terribly slow for large files, and an unwanted double-click on a file can cost quite some time in which Matlab is unresponsive.
Thanks.
When you click something in the Current Folder tab, it's actually running the open command, which itself calls finfo to determine what it means by "open" for a given extension. You can see this by creating a breakpoint in open.m directly after the line [~, openAction] = finfo(fullpath); and double clicking - when it hits the breakpoint you'll see it returns openAction as uiimport.
In theory, you can create custom methods for extensions by creating on the path a function openabc where abc is the extension, which should be returned as the openAction.
However, if I look at my finfo.m it first searches for said functions and then regardless of whether or not it finds them if there is an inbuilt method it overwrites them with the standard behaviour. There's even a comment:
% this setup will not allow users to override the default EXTread behavior
If you are willing to muck about in the inbuilts, you may be able to do it like this (backup first! - this could affect other things). I did it temporarily by shadowing the existing finfo like this:
edit finfo.m (Now save a copy to the current folder)
Add these lines after the loop that defines the openAction (in my version, around line 85):
if any(strcmp(['.' ext], matlab.io.internal.xlsreadSupportedExtensions))
openAction = 'winopen';
end
From the folder containing your edited finfo.m, type which finfo -all. You should see two copies, the MATLAB one labelled as shadowed. Opening something from the current folder window should now open Excel externally.
I don't believe there's any straightforward way to do that. It's built in to MATLAB that Excel files will open in the import tool when you double click on them, and there's no way to change that.
You might be able to get around it by changing the file extension on your Excel files to something other than .xls or .xlsx. That would stop MATLAB from opening it in the import tool. Then in Windows, you could associate the new file extension with Excel.

Visual Studio extension code completion- multiple file data

I'm looking to create code completion for a custom language in Visual Studio. I already have code for some simpler editor interaction, like syntax highlighting and brace matching. Now I want to move to code completion. But I've run into a slight problem- data sources.
Like in a language such as C#, I implicitly share code between more than one file. This implies that, in order to code complete one file, I need to know the contents of the other files. More specifically, whilst I could simply iterate through the project and project items and crack open the files, this is a suboptimal solution. For example, I wouldn't be able to code complete unsaved changes the user has made. For another, I already did a lot of processing work lexing or parsing the contents to fill the user's request for syntax highlighting and whatnot, and I have no desire to duplicate that work.
How can I access the contents of the other files in the project, and obtain their ITextBuffers so I can re-use the work I already did?
There is no ITextBuffer provided for files which exist on disk but are not currently open in Visual Studio. It is possible to create an instance of ITextBuffer for any arbitrary file by using ITextDocumentFactoryService::CreateAndLoadDocument. This takes a string and will give back an ITextDocument instance from which you can access an ITextBuffer.
In order to find the existing ITextBuffer for files that are currently open you can do the following
IVsRunningDocumentTable::FindDocument This takes a file path and returns a cookie representing that file if it is currently open (Example)
IVsRunningDocumentTable::GetDocumentInfo This takes a cookie and returns back an IVsTextLines instance (Example)
IVsEditorAdaptersFactoryService::GetDataBuffer takes an IVsTextLines and gives back an ITextBuffer (Example)

Resources