vscode.workspace.openTextDocument fails silently - visual-studio-code

With the same value for Uri, openTextDocument fails to have any discernible effect yet executeCommand successfully opens the document.
vscode.workspace.openTextDocument(uri);
vscode.commands.executeCommand("vscode.open", uri);
Are there any known problems with vscode.workspace.openTextDocument?

This might simply be a misunderstanding of what openTextDocument() does. It just creates a vscode.TextDocument instance, actually showing it in the UI is independent of that. That's why it's in the vscode.workspace namespace rather than vscode.window.
vscode.window.showTextDocument is used for actually showing a document:
Show the given document in a text editor. A column can be provided
to control where the editor is being shown. Might change the active editor.
vscode.workspace.openTextDocument(...).then(
document => vscode.window.showTextDocument(document));

Related

Word addin, set filename

If one starts a blank file on Word you can usually see in the top bar a name such as "Document1", "Document2" and so on.
Yet, if you attempt to open a file using the Word JS API like this:
Word.run((context) => {
context.application.createDocument(documentB64).open()
return context.sync()
})
The top bar comes out like this:
No filename is set.
To set a filename/handle(?), I tried using the code given here Office JS - Add customProperty to new document but that didn't help.
My addin is usually used in conjunction with another (VSTO) add-on and that add-on can't work properly with the documents opened by my addin and I believe the lack of a filename (/handle?) explains it to some extent.
Is there something I can do about this?
Thank you
Currently you can't do this because the newly created document is just a temporary file and not saved. can you try to call the following code to make sure the newly created file is saved?
const documentCreated = context.application.createDocument(externalDoc);
documentCreated.save();
documentCreated.open();

Error handling in Sublime Text Plugin API

I'm creating my first sublime plugin to be used internally and was wondering if there's a way to stop the plugin from executing and display an error message on screen like an alert?
I took a look at view#show_popup() but this didn't render for me. Below is how I attempted to show it:
if "WebContent" in subdirectories:
directory = ROOT_DIR + "/WebContent"
elif "Content" in subdirectories:
directory = ROOT_DIR + "/Content"
else:
self.view.show_popup('Directory not found', location=-1)
Working Principle:
The plugin takes some data from one view and then pastes them in another view. The plugin has two TextCommands. One command takes the data from the first view, opens a new view and then executes the 2nd command on the new view. The above snippet is in the 2nd command.
I was unable to find any resources to help with show_popup() or any other error handling.
Does any one have possible ideas?
view.show_popup() is for showing things like hover popups next to the cursor; it's used for things like the built in functionality for going to references/definitions for functions:
While you could in theory use this for error messages, it may not be the sort of user experience that anyone would expect.
Your code above is technically valid, but since the content is expected to be minihtml it may be hard to see because as just a single string all you're going to see is just the text (i.e. you have no font style, padding, etc):
Perhaps more importantly, when location is -1, the popup appears at the point in the buffer closest to the first cursor position, so depending on your circumstance it may be appearing in a place you don't expect, and then vanishing away before you can scroll to see it, etc.
What you want here is sublime.error_message(); given a string, it will display that string in a dialog for the user to interact with, and it also logs the error into the Sublime console as well so that there's an additional trace.

VSCode API: Anticipate or combine document text changes

Let's say I want to make an extension that listens for document changes and always applies them twice. If the user types 'w' it will turn out as 'ww', if the user backspaces once, it should actually backspace twice. If the user pastes something, it should be pasted twice. (My real use case is more complicated, but it is similar to this)
I could listen for document changes using
vscode.workspace.onDidChangeTextDocument(event => {
// Do something
});
and use event.contentChanges to see what changes were made and then use
activeEditor.edit((editBuilder: vscode.TextEditorEdit) => {
// Do something
});
to make the same edit to the document again.
The problem is that this effectively makes two changes to the document so the user will need to ctrl+z twice to undo what is seen as a single action from the user's perspective.
What would be great is if I could anticipate text changes (and then just append more changes) through some method like vscode.workspace.onWillChangeTextDocument but this does not exist (vscode issue #74381, #44771). I also described my problem there.
I see that some people override the 'type' command in vscode to execute before any edit is made (example). But this does not give me access to the edit I want to alter, nor does it capture backspaces or copy/paste commands.
I also tried uding a formatter but this will seemingly override other formatters and users will need to enable "formatting on type" (manually?) for it to update as often as I want. That will also mess with unnecessary user settings/workflows/extensions.
Any idea of how I can achieve this?
The undesired behavior with undo/redo can be mitigated by passing an options object to activeEditor.edit that prevents an "undo stop" to be added before my 2nd edit/change/action. Like this:
activeEditor.edit((editBuilder: vscode.TextEditorEdit) => {
// Do something
}, {undoStopBefore: false, undoStopAfter: false});
undoStopAfter seems like it could be either true or false, producing identical behavior for now.

In a VS Code extension, how can I be notified when the user cuts/copies/or pastes?

For my extension I need to know when a cut/copy/paste happens and be able to get the text associated with those operations. I can probably get the text from the editor if I know when they happen.
I cannot find a listener for these operations. I suppose I can look for ctrl-x, ctrl-c, and ctrl-v keyboard inputs but some users may use the edit menu and not use the keyboard.
Is there a way to be notified when these operations happen either from the keyboard or the edit menu?
Original asker here...
I came up with a solution that involves overriding the default cut/copy/paste actions in the editor. Here is the code for 'copy' in extension.js (I am using js not ts):
//override the editor.action.clipboardCopyAction with our own
var clipboardCopyDisposable = vscode.commands.registerTextEditorCommand('editor.action.clipboardCopyAction', overriddenClipboardCopyAction);
context.subscriptions.push(clipboardCopyDisposable);
/*
* Function that overrides the default copy behavior. We get the selection and use it, dispose of this registered
* command (returning to the default editor.action.clipboardCopyAction), invoke the default one, and then re-register it after the default completes
*/
function overriddenClipboardCopyAction(textEditor, edit, params) {
//debug
console.log("---COPY TEST---");
//use the selected text that is being copied here
getCurrentSelectionEvents(); //not shown for brevity
//dispose of the overridden editor.action.clipboardCopyAction- back to default copy behavior
clipboardCopyDisposable.dispose();
//execute the default editor.action.clipboardCopyAction to copy
vscode.commands.executeCommand("editor.action.clipboardCopyAction").then(function(){
console.log("After Copy");
//add the overridden editor.action.clipboardCopyAction back
clipboardCopyDisposable = vscode.commands.registerTextEditorCommand('editor.action.clipboardCopyAction', overriddenClipboardCopyAction);
context.subscriptions.push(clipboardCopyDisposable);
});
}
This definitely doesn't feel like the best solution... however it does seem to work. Any comments/suggestions? Are there any issues that repeatedly registering and unregistering will cause?
There is no api to access the clipboard directly but some extensions override the default copy and paste shortcuts to customize the copy paste behavior. Here are two examples:
https://github.com/aefernandes/vscode-clipboard-history-extension/blob/master/src/clipboard.ts
https://github.com/stef-levesque/vscode-multiclip/blob/master/src/extension.ts
As you note, that approach will not work when copying using the context menu however. For supporting that as well, you could try intercepting the editor.action.clipboardCopyAction command. See how the Vim extension intercepts the type command for an example of this: https://github.com/VSCodeVim/Vim/blob/aa8d9549ac0d31b393a9346788f9a9a93187c222/extension.ts#L208
There is a proposed api in v1.68 for intercepting and modifying pastes. Since it is proposed for now you can only test it in the Insiders Build.
See copy/paste proposed extension api:
The new documentPaste API proposal lets extensions hook into copy
and paste inside text editors. This can be used to modify the text
that is inserted on paste. Your extension can also store metadata when
text copy and use this metadata when pasting (for example for bringing
along imports when pasting between two code files).
The document paste extension
sample
shows this API in action: <-long code example in the release notes->
Here is the actual (brief) api: proposed documentPaste api
See also using a proposed api

LibXML: Comment-out a block of Elements

IS there a way to add/initate a comment ( e.g. $dom->createComment ... ) such that it comments out an entire block of xml tags. Basically I want to turn-off the content between the comment.
For example, it would look like this:
<TT>
<AA>keep</AA>
<!-- comment to blocking
<BB>hideme1</BB>
<CC>hideme2</CC>
-->
<DD>d's content is good</DD>
</TT>
Actually this question is a pre-cursor to my attempt to figure-out a method to be able to markup/label/identify the changes to an xml files in support of new client software functionality, but be able to have the ability to remove / back-out these xml changes in the rare event the client needs to fall back to the previous software version (and no I can't just simply point back to the original xml file because the client is allowed to make minor modifications to existing node text values). This is all going to be controlled via a perl script and LibXML's core modules (I can't use modules the client doesn't have).
So basically I've identified three possible types of xml changes as a result of new client sw functionality:
1.) ADD new element node(s) (typically to support new sw functionality)
2.) DELETE element node(s), or blocks of (would be rare, but never-the-less a possibility)
3.) CHANGE node text values (rare, but the new sw may require a new value)
For all three types, the client needs the ability to back out the changes. One thing I was thinking to use is ATTRIBUTES since the existing xml files don't use them. For example, for an ADD change type, I could include an atribute like 'ADD="sw version 4.1"' . This way if it needs to be removed, I could just simply have the perl script find those attribute strings and delete them (using LibXML methods). Same thing with CHANGE change type - I could use an attribute like CHG="newvalue_oldvalue", then again use straight perl (or LibXML) to switch back the value based on the contents of the attribute. The DELETE change type is giving me a problem though (as welll as the others lol!). I want to be able to "keep" the deleted lines in the xml file soley for the purposes if the sw falls back a version (at some late point the perl script could eventually cleanup/delete them).
I know this is a lot, I'm new to LibXML (but not to perl). I was just wonder if any of you have any thoughts as to how to go about it or seen anything resembling this kind of request ... I'd be grateful for any kind of advice! Thank you...