How to use built-in Javascript extension to automatically find symbols in a JS file when developing a custom extension? [duplicate] - visual-studio-code

This question already has an answer here:
Execute "go to Symbol in File" programmatically in vscode?
(1 answer)
Closed 3 years ago.
I'm writing a custom VSCode extension. I need to grab symbols (e.g. variables, functions, etc.) from a Javascript file that the user is currently on to find their definitions and do something with them when the user hovers over. Does VSCode / the JS extension expose an API to find these symbols and their definitions without doing so manually?
None of the API (https://code.visualstudio.com/api/references/vscode-api) seems to expose this functionality.
I've tried looking at the built in JS/TS extension to see what kind of API it exposes but found nothing.

async function getSymbols(document: vscode.TextDocument): Promise<vscode.DocumentSymbol[]> {
return await commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', document.uri) || [];
}
https://code.visualstudio.com/api/references/commands

Related

Get Visual Studio Code snippets scope programmatically using API

Can I get the list of the available scopes for snippets?
Is there any native functionality that would control what files to create and where?
Snippets are scoped to languages and introduced to VS Code as global or local to a workspace. Languages can be contributed to VS Code by exceptions. Typically by declaring a language in the package.json, adding a syntax and snippet files. Update:
Here is the guide how to add a snippet to any language via your extension. If you follow the guide and add a snippet to the current workspace, you will notice that the file with extension .code-snippets gets created under the .vscode folder in the workspace. Therefore you can enumerate all such files using the vscode.workspace.workspaceFolders and fs.readDir APIs.
vscode.workspace.workspaceFolders!.forEach(workspaceFolder => {
const snippetFiles = fs.readdirSync(path.join(workspaceFolder.uri.fsPath, '.vscode'))
.filter(el => /\.code-snippets$/.test(el));
});
Similarly, the global snippets get added as .code-snippets files into the user profile folder - on Windows it looks like this: C:\Users\currentUser\AppData\Roaming\Code\User\snippets\*.code-snippets. Both locations you can scan for *.code-snippets files as well as programmatically add such files to those folders.
Here is the documentation how to add snippets to a scope via a file. There does not seem to be any public API to enumerate all such snippet files in a programmatic way.
But if you want to retrieve list of the languages in the screenshot you posted, use this API:
vscode.languages.getLanguages()
It could help if you explained what are you trying to achieve. Do you want to find where a particular snippet is coming from to change it? Do you want to add a snippet to a language in this list? Or to a language that is not on this list?

How to import custom css and js in Ionic 4 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am using a custom theme for my app in ionic. But it has some css and javascript dependencies except bootstrap.
Could you help me how can I import these libraries to my app?
Regards
It sounds like you are probably missing some Angular usage theory if you are trying to import Bootstrap into your project.
It uses a specific set of classes and they aren't out-of-the-box compatible with Angular / Ionic.
Ionic will give you a cross-device baseline set of styles so you should really work with Ionic to build upon their styles rather trying to fight it with another system.
However, to answer your questions:
Importing CSS
You can edit app.scss and reference files in your imported packages like this:
#import '~#swimlane/ngx-datatable/release/assets/icons';
Notice the ~ at the start and then the package name, then the path to the file. You don't need to put the .css or .scss on the end but you can if you want to.
Importing JS
I'm not sure the best way to do this, but you can include scripts in the <head> tag by editing the index.html.
It's not something that's really done though and sounds like you are misunderstanding the way Angular / Ionic works.
It's based around building web components and you let Angular manage the actual live html. Your code that you write is TypeScript and its to interact with various API's that set the data model and then you let Angular generate the actual script.
There is library for loading asynchronous JavaScript files. https://www.npmjs.com/package/scriptjs
Install the package:
npm i scriptjs
Then use it anywhere like below:
import { get } from 'scriptjs';
ngOnInit() {
get("assets/js/searchEmp.js", () => {
getSerchInspContext = this;
loadSearchEmp();
});}`
OR
You can simply use the jquery method to append or remove the script tag in your header.
To add .js file, call below line under ngOnInit():
$('head').append('<script async src="assets/js/search.js"></script>');
Remove .js file:
document.querySelector('script[src="assets/js/search.js"]').remove();

Can not import and use DirectorySearch in atom.io

I'm trying to build a package for atom editor, I need to search for all local files in the project.
From https://atom.io/docs/api/v1.0.2/DirectorySearch I see DirectorySearch is an interesting class to search for specific text in local files.
There is little documentation on the page. I tried {DirectorySearch} = require 'atom' and new atom.DirectorySearch(). But they are not working, said "DirectorySearch is undefined".
I searched in atom's repository, but it seems that they only defined it. There is no usage of DirectorySearch. I also searched on Google and Stack Overflow but with no luck.
I'm using Version 1.0.2 on Mac OSX 10.
Can someone tell me how to import and use this class?
According to https://discuss.atom.io/t/how-to-import-and-use-directorysearch-in-atom/19205
Looking at the source it doesn't seem you can require it, but there's an instance of DefaultDirectorySearcher created in the workspace and available at atom.workspace.defaultDirectorySearcher that is used in the scan method as a fallback when a searcher for a directory haven't been specified.
If you want to search text in files, atom.workspace.scan should be enough.
You can also register a custom directory searcher using the atom.directory-searcher service, as far as I can tell, the object needs to implement the following methods to comply to the searcher interface:
-canSearchDirectory (directory:Directory) -> Boolean
-search(directories:Array, regex:RegExp, searchOptions:Object) -> CancellablePromise
The DirectorySearch class that appears in the docs is actually the CancellablePromise returned by the directory searcher.

vscode warning for included js obj

I have 2 question about the edit visual studio code.
1. when I include a js file in my html as <script src="hey.js"></script> and use it in my js code after like hey.speak() the editor says 'cannot find name 'hey'.
when I create new function and calling this function afterward when it shows me the function's argument why it says "any" on every argument it takes ? when In real the current function expects to get a function (in a callback case)
I'm trying to arrange my project code, and I'm trying to follow the wornings. thanks (:
VSCode doesn't load <script> tags referenced in HTML automatically. If you open the referenced file by yourself (e.g. in an editor to the side), global symbols should get picked up.
This is definitely a nice feature request, you can ask for it at the VSCode User Voice Website.
In the meantime, you can configure the linting settings of JavaScript, to ignore undeclaredVariables, for example.
You should be able to include
/* global hey */
to the top of your script, then VS Code (and other linters) will know that you have an undeclared global the you will be importing.

How could I include a plugin system for my Dart app?

I have a Qt application containing a Webkit module and using Dart (compiled to JS). It's like a bare-bones browser written in Qt. The application basically replaces certain text on the webpage with different text. I want users to be able to make their own Dart files to replace their own text with their own different text.
Any recommendations for approaches to creating a plugin system?
I think that this question needs a little clarification: are you asking about using Dart for scripting Qt applications (where Dart plays the role of a scripting language), or are you asking about a plugin system for Dart application that is compiled to JS and used in a Qt application, probably via QtScript (in which case, the role of a scripting language is played by JavaScript)?
I presume that it is the latter variant (and I don't know enough about Qt to be able to answer about the former variant anyway).
Let's assume that all plugins for the Dart application are available at the build time of that Qt application, so that you don't need to compile Dart to JS dynamically. Then, if you compile a Dart script, resulting JS will include all necessary code from its #imports. All you need is to create a proper script that imports all plugins and calls them (importing isn't enough, as dead code will be eliminated).
Maybe an example will be more instructive. Let's say that you want to allow plugins to do some work on a web page. One way you might structure it is that every plugin will be a separate #library with a top-level function of a well known name (say doWork). Example of a plugin:
// my_awesome_plugin.dart
#library('My Awesome Plugin')
doWork(page) {
page.replaceAll('JavaScript is great', 'Dart is great');
}
You can have as many plugins of this nature as you wish. Then, you would (at the build time) generate a following simple main script in Dart:
// main.dart
// these lines are automatically generated -- for each plugin file,
// one #import with unique prefix
#import('my_awesome_plugin.dart', prefix: 'plugin1');
#import('another_plugin.dart', prefix: 'plugin2');
main() {
var page = ...; // provided externally, from your Qt app
// and these lines are automatically generated too -- for each plugin,
// call the doWork function (via the prefix)
plugin1.doWork(page);
plugin2.doWork(page);
}
Then, if you compile main.dart to JavaScript, it will include all those plugins.
There are other possibilities to structure the plugin system: each plugin could be a class implementing a specific interface (or inheriting from a specific base class), but the general approach would be the same. At least the approach that I would recommend -- making each plugin a separate library.
You probably don't like the step with generating the main script automatically, and I don't like it either. But currently, Dart only allows one way to dynamically load new code: spawning new isolates. I'm not sure how (or even if) that would work in QtScript, because isolates are implemented as web workers when compiled to JavaScript, so I won't discuss this here.
Things will get more complicated if you want to support compiling Dart scripts at the runtime of your Qt application, but I think that I'm already guessing too much about your project and I might be writing about something you don't really need. So I'll finish it like this for now.