How to get existing SourceControl via VSCode extension API? - visual-studio-code

The VSCode extension API provides a means of creating a new SourceControl. Is there any way to ask for an existing SourceControl for the WorkspaceFolder? I'd like to be able to learn about scm file status from an extension without having to recreate existing version control plugins; instead, I'd like to be able to asking the existing plugins.

Very late answer by as I had the same question a while ago I thought I should share what I found.
At that time, I was trying to build an extension to get the remote URL for the current file opened in the editor, so I used the default Git integration for VSCode after some research as the documentation doesn't make this very easy to find.
Here's a little snippet of how you can use it to extract information about the current repository:
const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git').exports;
const git = gitExtension.getAPI(1);
const repository = git.getRepository('current-workspace-path');
const { remotes, HEAD } = repository.state;
for (const remote of remotes) {
const { fetchUrl } = remote;
}
You can get the current workspace path using the workspace (there might be a better way though):
const workspaceRootUris = vscode.window.workspaceFolders.map(ws => ws.uri);

Related

How to customize addContentItemDialog to restrict files over 10mb upload in IBM Content Navigator

I am customizing ICN (IBM Content Navigator) 2.0.3 and my requirement is to restrict user to upload files over 10mb and only allowed files are .pdf or .docx.
I know I have to extend / customize the AddContentItemDialog but there is very less detail on exactly how to do it, or any video on it. I'd appreciate if someone could guide.
Thanks
I installed the development environment but I am not sure how to extend the AddContentItemDialog.
public void applicationInit(HttpServletRequest request,
PluginServiceCallbacks callbacks) throws Exception {
}
I want to also know how to roll out the changes to ICN.
This can be easily extended. I would suggest to read the ICN red book for the details on how to do it. But it is pretty standard code.
Regarding rollout the code to ICN, there are two ways:
- If you are using plugin: just replace the Jar file on the server location and restart WAS.
- If you are using EDS: you need to redeploy the web service and restart WAS.
Hope this helps.
thanks
Although there are many ways to do this, one way indeed is tot extend, or augment the AddContentItemDialog as you qouted. After looking at the (rather poor IBM documentation) i figured you could probably use the onAdd event/method
Dojo/Aspect#around allows you to do exactly that, example:
require(["dojo/aspect", "ecm/widget/dialog/AddContentItemDialog"], function(aspect, AddContentItemDialog) {
aspect.around(AddContentItemDialog.prototype, "onAdd", function advisor(original) {
return function around() {
var files = this.addContentItemGeneralPane.getFileInputFiles();
var containsInvalidFiles = dojo.some(files, function isInvalid(file) {
var fileName = file.name.toLowerCase();
var extensionOK = fileName.endsWith(".pdf") || fileName.endsWith(".docx");
var fileSizeOK = file.size <= 10 * 1024 * 1024;
return !(extensionOK && fileSizeOK);
});
if (containsInvalidFiles) {
alert("You can't add that :)");
}else{
original.apply(this, arguments);
}
}
});
});
Just make sure this code gets executed before the actual dialog is opened. The best way to achieve this, is by wrapping this code in a new plugin.
Now on creating/deploying plugins -> The easiest way is this wizard for Eclipse (see also a repackaged version for newer eclipse versions). Just create a new arbitrary plugin, and paste this javascript code in the generated .js file.
Additionally it might be good to note that you're only limiting "this specific dialog" to upload specific files. It would probably be a good idea to also create a requestFilter to limit all possible uses of the addContent api...

How to get access to Developer: Inspect TM Scopes data? [duplicate]

I have used the tokenizer in monaco but I do not see that it is accessible in vscode. This would be helpful for completion/signature help providers, how can I tokenize a grammar?
It doesn't seem like there's an official way of doing this right now. There is an open feature request for adding the ability to retrieve tmLanguage scopes at a position here: #580
There is one potential workaround, which requires adding a dependency to the scope-info extension. This extension exposes an API of its own that other extension can use. Here's a code example posted by the author in the linked issue:
import * as api from 'scope-info'
async function example(doc : vscode.TextDocument, pos: vscode.Position) {
const siExt = vscode.extensions.getExtension<api.ScopeInfoAPI>('siegebell.scope-info');
const si = await siExt.activate();
const t1 : api.Token = si.getScopeAt(doc, pos);
}
Update: unfortunately, it looks like scope-info is no longer compatible with current VSCode versions.

Testing VSCode extensions - how to verify that decorations are set

I have an extension which adds text decorations at the end of some of the lines. I'd like to write a test which verifies that the text decorations are added and also to assert in the test that their value is correct.
suite('Extension Tests', () => {
test('Should work', async() => {
const fixturePath = path.join(__dirname, '..', '..', 'test', 'fixtures');
const uri = vscode.Uri.file(path.join(fixturePath, 'a.js'));
const document = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(document);
window.activeTextEditor.getDecorations(); //???
});
});
It seems that the getDecorations API is missing from editor. What can I do to resolve this?
As of VSCode 1.37.1 (2019-08-30), the answer is no, there is no way to write an automated test for decorations using the decorations extensions API.
There are automated tests of decorations in the vscode sources, particularly modelDecorations.test.ts, but they use private APIs.
Microsoft has an example extension that uses decorators, decorator-sample, but it has no tests.
There are a few open issues related to this, although none directly addresses the inability to enumerate existing decorations:
50346: Lacking decorations-related APIs (mainly about incremental update for better performance); closed as duplicate of:
585: Provide an API for advanced/semantic source highlighting
50415: Missing API for knowing when an extension's decorations get trashed
54938: Make decoration provider API public
Therefore I recommend filing a new issue.
2022-04-27: As noted in a comment, issue 136164: Provide Access to a TextEditors Decorations for Testing Purposes was filed on 2021-10-30. It was then closed as unnecessary because you can test that your extension calls the extension API as intended. I find this response inadequate because there's no way to test that the effect is as intended (for example, TextEditor.setDecorations removes existing decorations that have the same type, and that's a subtle interaction that would only be revealed by examining the effects), but the issue is locked so yet another issue would have to be filed to raise an objection.

How to tokenize grammars, like in monaco.editor.tokenize for use in extension

I have used the tokenizer in monaco but I do not see that it is accessible in vscode. This would be helpful for completion/signature help providers, how can I tokenize a grammar?
It doesn't seem like there's an official way of doing this right now. There is an open feature request for adding the ability to retrieve tmLanguage scopes at a position here: #580
There is one potential workaround, which requires adding a dependency to the scope-info extension. This extension exposes an API of its own that other extension can use. Here's a code example posted by the author in the linked issue:
import * as api from 'scope-info'
async function example(doc : vscode.TextDocument, pos: vscode.Position) {
const siExt = vscode.extensions.getExtension<api.ScopeInfoAPI>('siegebell.scope-info');
const si = await siExt.activate();
const t1 : api.Token = si.getScopeAt(doc, pos);
}
Update: unfortunately, it looks like scope-info is no longer compatible with current VSCode versions.

How can I Diff a Svn Repository using SharpSvn

My question is quite simple and with the SharpSvn Api, it should be easy as well. Here what I did:
path = "c:\project";
using (SvnLookClient client = new SvnLookClient())
{
SvnLookOrigin o = new SvnLookOrigin(path);
Collection<SvnChangedEventArgs> changeList;
client.GetChanged(o, out changeList); // <-- Exception
}
and when I call the GetChanged, I get an exception:
Can't open file 'c:\project\format': The system cannot find the file specified.
So, Maybe there is something I'm missing? Or maybe it's not the right way to do find out the list of files and folders that were modified in the local repository?
Thanks in advance.
The SvnLookClient class in SharpSvn is the equivalent to the 'svnlook' console application. It is a low level tool that enables repository hooks to look into specific transactions of a repository using direct file access.
You probably want to use the SvnClient class to look at a WorkingCopy and most likely its Status() or in some cases simpler GetStatus() function to see what changed.
The path that the SvnLookOrigin constructor wants is actually:
path = "c:\project\.svn\";
That is, it wants that special ".svn" directory not just the root of where the source is checked out to.
Although you probably do want to listen to Bert and do something like:
path = "c:\project";
using (SvnLookClient client = new SvnLookClient())
{
SvnLookOrigin o = new SvnLookOrigin(path);
Collection<SvnChangedEventArgs> changeList;
client.GetStatus(o, out changeList); // Should now return the differences between this working copy and the remote status.
}