Is there auto-import functionality for typescript in Visual Studio Code? - visual-studio-code

Is there any shortcut that will allow me to auto generate my typescript imports? Like hitting ctrl+space next to the type name and having the import declaration placed at the top of the file. If not, what about intellisense for filling out the module reference path so that I wont have to do it manually? I would really love to use vscode but having to do manual imports for typescript is killing me.

There are rumors of making it support tsconfig.json (well, better than rumors). This will allow us to be able to use all files for our references.
Your feature would be to create an auto import of all commonly used 3rd party libs into the typings. Perhaps auto scan the files and create a list of ones to go gather. Wouldn't it be fine to just have a quick way to add several of these using tsd directly from Code (interactively)?

I believe the plugin called "TypeScript Importer" does exactly what You mean: https://marketplace.visualstudio.com/items?itemName=pmneo.tsimporter .
Automatically searches for TypeScript definitions in workspace files and provides all known symbols as completion item to allow code completion.
With it You can truly use Ctrl+Space to choose what exactly You would like to be imported.
You can find and install it from Ctrl+Shift+X menu or just by pasting ext install tsimporter in Quick Open menu opened with Ctrl+P.

I know a solution for Visual Studio (not Visual Studio Code, I'm using the 2015 Community edition, which is free), but it needs some setup and coding -- however, I find the results to be adequate.
Basically, in Visual Studio, when using the Web-Essentials extension, .ts files can be dragged into the active document to automatically generate a relative reference path comment:
/// <reference path="lib/foo.ts" />
With which of course we might as well wipe it, because it's an import statement we need, not a reference comment.
For this reason, I recently wrote the following command snippet for Visual Commander, but it should be easily adaptable to other use casese as well. With Visual Commander, your drag the needed imports into the open document, then run the following macro:
using EnvDTE;
using EnvDTE80;
using System.Text.RegularExpressions;
public class C : VisualCommanderExt.ICommand
{
// Called by Visual Commander extension.
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
TextDocument doc = (TextDocument)(DTE.ActiveDocument.Object("TextDocument"));
var p = doc.StartPoint.CreateEditPoint();
string s = p.GetText(doc.EndPoint);
p.ReplaceText(doc.EndPoint, this.ReplaceReferences(s), (int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers);
}
// Converts "reference" syntax to "ES6 import" syntax.
private string ReplaceReferences(string text)
{
string pattern = "\\/\\/\\/ *<reference *path *= *\"([^\"]*)(?:\\.ts)\" *\\/>";
var regex = new Regex(pattern);
var matches = Regex.Matches(text, pattern);
return Regex.Replace(text, pattern, "import {} from \"./$1\";");
}
}
When running this snippet, all reference comments in the active document will be replaced with import statements. The above example is converted to:
import {} from "./lib/foo";

This has just been released in version 1.18.
From the release notes:
Auto Import for JavaScript and TypeScript
Speed up your coding with auto imports for JavaScript and TypeScript. The suggestion list now includes all exported symbols in the current project. Just start typing:
If you choose one of the suggestion from another file or module, VS Code will automatically add an import for it. In this example, VS Code adds an import for Hercules to the top of the file:
Auto imports requires TypeScript 2.6+. You can disable auto imports by setting "typescript.autoImportSuggestions.enabled": false.

The files attributes in the tsconfig.json file allows you to set your reference imports in your whole project. It is supported with Visual Studio Code, but please note that if you're using a specific build chain (such as tsify/browserify) it might not work when compiling your project.

Related

Why isn't info popping up when the mouse hovers over a function in a package

When the mouse pointer hovers over the name of a function created in my code a box pops up in the editor with information about that function. However, if the function is in a package nothing happens. For example, if the mouse pointer hovers over the word "DataFrame" in the second line of the code below no box with info pops up. This is not specific to package DataFrames.
using DataFrames
DataFrame(:A => [0])
In VSCode Settings the following is set:
Editor > Hover:Delay
200
Editor > Hover:Enabled
Checked
The IDE is Visual Studio Code version 1.71.2 and the OS is Windows 11. The programming language is Julia, version 1.8.1.
It used to work, something changed, I do not know what.
Any hints on why this is happening?
I believe I found the solution. I created a function that adds a package to the environment. It has the package name as its single argument.
function load_package(package_name::String; used = true, report = true)
str_ui = used ? "Using " : "Importing "
report && println(str_ui * "package $package_name")
if used
eval(Meta.parse("using $package_name"))
end
eval(Meta.parse("import $package_name"))
end
It works fine. The package is loaded. For example,
load_package("DataFrames")
does (almost) the same thing as
using DataFrames
The only difference is that Visual Code does not seem to notice that the package was loaded when the package is loaded with the loaded_package function but it does notice it when the package is loaded with the using command.
So to fix the problem I had to load my packages with using and import comands. After doing that the tooltips became visible upon hovering over the function names.
The reason I was using the function load_packages was to load sets of packages whose names were in string vectors.

VSCode - auto_prepend_file for code suggestions

I have been using VS code instead of eclipse for the last few weeks and find it much quicker, easier to use. However there is one thing I can’t seem to figure out.
My php app calls a prepend file which initiates a class called GlobalClass:
$gc = new GlobalClass();
When I type $gc-> in eclipse I get all the functions prompted. This doesn’t work within VS Code.
If I add use GlobalClass or add the $gc = new GlobalClass(); to the top of the file then it works. Is there any way to declare this within VS Code or to point the editor to the code within the prepend file?

initRelector is defined in multiple libraries

I'm trying to export my various component templates from a templates.dart file. For example export './src/foo/foo_component.template.dart';
export './src/bar/bar_component.template.dart';
The VSCode analyzer is complaining "The name 'initReflector' is defined in the libraries
package:my_app/src/foo/foo_component.template.dart and package:my_app/src/bar/bar_component.template.dart"
I'd suggest only showing what you need from those libraries instead of exporting everything that is public:
export './src/bar/bar_component.template.dart' show BarComponentNgFactory;
If that seems too much of a hassle you could go the other way and just hide initReflector.

How do I disable Visual Studio Code's C++ access modifier indent?

Version 1.15.0 of vscode seems to aggressively change the indenting of access modifiers in C++ code, despite disabling autoIndent and formatOnType.
When I enter this:
class Foo
{
public:
Foo();
};
It gets reformatted as soon as I hit enter after typing "public:", to this:
class Foo
{
public:
Foo();
};
In my user settings I have "editor.autoIndent" and "editor.formatOnType" set to false. I also have "C_Cpp.formatting" in the C++ extension set to "Disabled".
Is there some way to control this behavior?
Have you tried to set this option to false?
"C_Cpp.clang_format_formatOnSave": false,
Edited
Also take a look at this option:
editor.formatOnSave
I found a way to do this by modifying the language configuration file for C++. It is located in the Microsoft VS Code directory here: resources/app/extension/cpp/language-configuration.json.
I removed the items in the "increaseIndentPattern" and "decreaseIndentPattern" that were related to access modifiers.
This works, but editing the file directly seems like a bad idea.

OnSave actions in NetBeans 6.9

Is there any way to tell NetBeans to execute a specific action when saving a file? e.g. removing unused imports while saving a source file?
This was an interesting question... as I believe you would have to write a custom NetBeans plugin to do what you're wanting (the functionality isn't available out-of-the-box), and I've been looking for an excuse to explore NetBeans plugin development.
However, after spending a couple of hours reading tutorials and crawling through the javadocs... it's become clear that this subject is quite a big bite to chew, and probably way more involved than you're wanting.
I think the BEST suggestion is forget about removing unused imports at save-time, and instead perform this step at build-time. NetBeans offers great integration with Ant and/or Maven (for build purposes it's basically just a GUI wrapper around those tools), and there are a number of Ant tasks out there that can do what you're wanting. See:
http://ant.apache.org/external.html
(look for the "CleanImports" and "Importscrubber" tasks)
If your NetBeans project(s) are Maven-based, then you can always plug in one of these Ant tasks there using the AntRun plugin for Maven.
If you're not used to dealing with Ant or Maven directly in NetBeans, then just switch to the "Files" tab and look at your project's root directory. If its a Maven project, the build script will be named pom.xml. Otherwise, your project will generally be Ant-based and the build script will be named build.xml. The documentation for these items above should make it fairly clear how to move forward from there.
I notice that those two Ant tasks haven't been updated in awhile, so if you run into issues you might want to check out the very popular and up-to-date PMD system, which has its own documentation for integrating with NetBeans. However, the issue there is PMD is primarily for generating reports... I don't know if it can be used to actually take action and change source files.
Not exactly an answer to your question, but note that NB 7.1 lets you fix imports on the whole project at once: http://wiki.netbeans.org/NewAndNoteworthyNB71#Organize_Imports_Hint
This is not a good practice and NetBeans does not support it.
I resurrect this topic.
Well this code code is tested with Netbeans 7.4.
here I'm overriding the default save action in the actionPerformed method.
If you choose to do this by yourself create a new Action using the wizard then call the save action inside actionPerformed method.
package yourpackage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
#ActionID(
category = "File",
id = "BZ.SaveAction"
)
#ActionRegistration(
iconBase = "BZ/Save.png",
displayName = "#CTL_SaveAction"
)
#ActionReferences({
#ActionReference(path = "Menu/File", position = 750),
#ActionReference(path = "Toolbars/File", position = 0),
#ActionReference(path = "Shortcuts", name = "D-S")
})
#Messages("CTL_SaveAction=Save")
public final class SaveAction implements ActionListener {
org.openide.actions.SaveAction sa = org.openide.util.actions.CallbackSystemAction.get(org.openide.actions.SaveAction.class);
#Override
public void actionPerformed(ActionEvent e) {
// custom code
JOptionPane.showMessageDialog(null, "custum message ");
sa.performAction();
}
}
Goto Tools-> Options select Editor there select On Save Tab now select Java from drop down menu. So, now select Organize Imports option. Hope this will help you.