Auto class importing in Sublime Text - sublimetext3

I'm looking to get auto class importing In Sublime Text. I'm using HaxeFlixel and when ever I add a private var _btnPlay:FlxButton the needed import doesn't automatically show up. How can I get this to be the default action? Is there maybe a hot key I don't know about?
I know in FlashDevelop it can be done as the tutorial I'm following said that was the case:
We need to define a new FlxButton variable to use as our 'play' button. So, type:
private var _btnPlay:FlxButton;
Note: if you're using FlashDevelop, it should automatically create an import for FlxButton (import flixel.ui.FlxButton;) at the top of the class. This should be mostly automatic whenever you use class, but if it doesn't add it for some reason, you can enter it manually, or, highlight FlxButton in the editor and hit Ctrl+Shift+1 to add it.

It looks like "Organize Imports" adds missing imports - here's the pull request where this feature was added.
The default shortcut for it requires pressing Ctrl+Shift+H and Ctrl+Shift+O. You might want to reassign it to something shorter, maybe just Ctrl+Shift+O:
[
{ "keys": ["ctrl+shift+o"], "command": "haxe_organize_imports" }
]

Related

How to force reimport of texture in godot?

I have a sample.png file which is being changed outside godot
and after it's modified, godot recives a signal and when that signal is received
I want that specific sample.png file to be reimported
I tried this answer but I want to reimport in my script itself not create a plugin for it
(atleast that's what I'm assuming it does)
I also tried this from the documents but I'm not sure how to use it exactly
EditorFileSystem.update_file("res://Assets/sample.png")
so how do I achieve the desired result?
The class EditorFileSystem is intended for plugins.
You would call get_editor_interface() from an EditorPlugin (which would be where you would be writing code if you were making a plugin). And that gives you an EditorInterface object, on which you can call get_resource_filesystem() which gives you an EditorFileSystem object.
So the intended use is something like this:
extends EditorPlugin
func example() -> void:
var editor_file_system := get_editor_interface().get_resource_filesystem()
editor_file_system.scan_sources()
# editor_file_system.update_file("res://icon.png")
By the way, EditorInterface also has a filesystem_changed signal. Although I don't know how reliable it is.
Usually you don't have to do that. When you restore the Godot window, it will scan for changes in the project folder. So you might minimize Godot while you are working on something else and when you bring the Godot window back it will pick on the changes.
In practice, the only situations when I had to use scan or scan_sources was when I had a tool script that write a resource file which should be imported, and I wanted it to reflect right away.
Instead of making a custom plugin, I'll remind you that form a tool script (as long as it is running in the editor) you can simply create an EditorPlugin object. For example:
var ep = EditorPlugin.new()
ep.get_editor_interface().get_resource_filesystem().scan()
ep.free()
I had also shared this example in another answer I wrote for you a while back here, it is under the title "About saving resources from tool scripts".

Adding content in file in specific place with node js (Like Angular Cli Modify app.module file)

I got the idea that node js its not just for web application for example I can create a console application with node (cli) .
and already I have an interest in how I can make a cli app that create files and modify existing files for example something like angular cli with one command "ng generate component" its :-
1- create a set of files
2- modify app.module file
a. add import statement for generated component
b. add generated component in declarations array
and after a lot of search I got that first step can be handled in some way with node file system module.
but i don't know how they modify "app.module" file by just adding some syntax in its right place for instance adding new import statement after all exists import statements also adding the component name in declarations array as a last item
I'm really appreciate any help maybe with some code example if possible and thanks in advance
After some searches i found this answer:
There a couple of ways of editing a file, the most reliable is perhaps
the most complex one which can be done by parsing the file (Generating
an abstract syntax tree) update the new ast and pass it to a code
generator which will output the new string (code) of the modified ast.
Another option is to use regular expressions to know where add to
certain statements. For example there would be a regex to match import
statements to lines, you will map the lines of the file to this regex
where you'll get an array of booleans denoting whether the line is an
import stmt. Or not, once the import stmts are finished you can insert
a new line with the new import statement in the original lines array.
A third option is to regenerate the file all at once everytime, but
this means that you'll have to a ctx of the project (ctx = object
contains some details.
and as reference i found that AST (Abstract Syntax Tree) is more reliable way also it's not that hard this is some links that helped me a lot to know what is AST in simple way and how to deal with it
1- What is AST and how to understand it and how to use it https://www.youtube.com/watch?v=tM_S-pa4xDk
2- and this is an amazing article about "Write Code to Rewrite Your Code with jscodeshift" https://www.toptal.com/javascript/write-code-to-rewrite-your-code
https://www.youtube.com/watch?v=tM_S-pa4xDk

How can I apply black code formatting on save?

I would like to apply black whenever I save a Python file in Sublime Text 3. How can I do that?
(Bonus points if there is a quick way to disable it)
The answer above is really nice. In case you do not want to write your own package or plugin and you do not like the Formatter package, there is also the sublack package, which I think supports quick enabling/disabling of running black on save.
You install sublack the usual way via package control (Ctrl-Shift-P (Mac: Cmd-Shift-P) Package Controll: Install package). Afterwards you can format the current file either manually:
Run Black on current file:
Press Ctrl-Alt-B to format the entire file. You can also Ctrl-Shift-P (Mac: Cmd-Shift-P) and select Sublack: Format file.
or you can:
Toggle Black on save for current view :
Press Ctrl-Shift-P (Mac: Cmd-Shift-P) and select Sublack: Toggle black on save for current view.
Alternatively, you can also enable to run black on save permanently, by creating a user setting (Preferences -> Package Settings -> sublack -> settings) similar to the following:
{
"black_on_save": true,
"black_line_length": 80,
}
python 3.6
pip install black
cmd + shift + p then Click Package Control: Install Package
sublack
On terminal, which python
Preferences/Package Settings/sublack/Settings
On right panel
{
"black_on_save": true,
"black_command": WHICH_PYTHON_RESULT
}
To do something like this you would need a package or plugin that is capable of triggering an external command whenever an on_post_save event is triggered in Sublime for a Python file.
The Formatter package is an example of a package that does this sort of thing and it's README mentions that it supports Black as well. I don't use that package myself so I can't recommend it one way or the other. There may be other packages that provide a similar functionality as well, although this is the only one I spotted that mentions that it supports Black explicitly.
In theory any formatting package or package to execute commands on save events that lets you specify the command to execute could likely be configured to work as well.
For the sake of completeness, a plugin to do something like this can be created fairly quickly by creating a ViewEventListener that only triggers inside of Python files and uses the internal exec command to execute the black command.
An example of such a plugin would be the following (this video provides instructions on how to set up a plugin in Sublime if you're unsure how to do that) which for meta points has been formatted on save by itself:
import sublime
import sublime_plugin
import os
class FormatWithBlackOnSave(sublime_plugin.ViewEventListener):
"""
Listen for file saves and run the black code formatter on Python files
as they are saved, unless they have a setting indicating that the autoformat
should be disabled.
"""
#classmethod
def is_applicable(self, settings):
return "/Python/" in settings.get("syntax") and not settings.get(
"black_disabled", False
)
def on_post_save(self):
path, file = os.path.split(self.view.file_name())
settings = sublime.load_settings("Preferences.sublime-settings")
show_panel_on_build = (settings.get("show_panel_on_build", True),)
override_panel = settings.get("black_override_panel", False)
env = settings.get("black_env", {})
args = settings.get("black_arguments", [])
if override_panel:
settings.set("show_panel_on_build", not show_panel_on_build)
window = self.view.window() or sublime.active_window()
window.run_command(
"exec",
{
"shell_cmd": 'black {args} "{file}"'.format(
file=file, args=" ".join(args)
),
"working_dir": path,
"env": env,
},
)
if override_panel:
settings.set("show_panel_on_build", show_panel_on_build)
Once that's in place in your User package, you should also add some custom settings to your Preferences.sublime-settings file to control it:
// When this is True, the plugin will not execute on save.
"black_disabled": false,
// Override the value of the `show_panel_on_build` setting that controls
// whether the output panel appears when the command is executed. When
// true the value of that setting is temporarily inverted.
"black_override_panel": false,
// The arguments (other than the current file) to pass to black
"black_arguments": [],
// Optional environment variables to use while running the tool
// (for example to set the path); works as in a `sublime-build` file.
"black_env": {
}
This requires you to install the black command yourself (e.g. pip install black) and will execute it with the given arguments for the current file, so long as the file is a Python file and the black_disabled setting is set to false as above.
The plugin uses the internal exec command, which uses the preference show_panel_on_build to determine if a panel that shows you the output of the command should appear or not. The default value for that setting is true, which means that every time you save a Python file the panel would open with a message like:
reformatted black.py
All done!
1 file reformatted.
[Finished in 0.2s]
The black_override_panel setting makes the plugin invert the value of the show_panel_on_build while it's executing the command; either not showing it when it normally would or vice versa.
Altering the setting in your preferences will globally disable the event listener from triggering in all Python files. You can also create a file in your User package with a name like Black.sublime-commands with the following content:
[
{
"caption": "Black: Toggle Format-on-save for this view",
"command": "toggle_setting",
"args": {
"setting": "black_disabled"
}
}
]
That would add a command to the command palette that inverts the state of the setting in the current file; that would allow you to disable the plugin only for certain files or enable it only for certain files.

Modify or extend default settings for a project

In a sublimetext3 project, is there a way to modify or extend, not replace, default settings?
To be specific, in a project I specify the paths of the folders to include in the project. Each folder has files and directories unique to that folder that I want to exclude using either folder_exclude_patterns or file_exclude_patterns; see documentation for Projects.
But as I understand this, these project settings replace not extend the default settings. What I would like, however, is to have a project setting that appends to the default pattern rather than replacing it. Is this possible?
Pseudo code that expresses what I would like to do:
"folders":
[
{
"path": "c:\\dir1",
"folder_exclude_patterns": default_folder_exclude_patterns + ["junk"]
},
{
"path": "C:\\dir2"
"folder_exclude_patterns": default_folder_exclude_patterns + ["old"]
},
]
If this is not possible, then I believe the only thing I can easily do is copy the default settings and replicate them for each folder item. Since I have multiple projects/folders and need to do this for file exclude, folder exclude and binary file settings, this will get tedious and be hard to maintain. Of course, this seems like it is ripe for a plugin, but that is not in the scope of what I am looking to do. (Of course if someone else has a plugin that does something like this, I would be happy to try it out! :-))
Unfortunately, due to the way Sublime is set up, higher-precedence settings replace lower-precedence ones, not supplement them. This is a good thing because many settings are either/or - what would you do if your user settings had "highlight_line": false while a project had "highlight_line": true, for example?
A plugin should be able to do the trick. sublime.Window contains the project_data() and set_project_data() methods, which allow you to retrieve and write project settings, respectively. You could add a "more_folder_exclude_patterns" key to each folder in your project with the additional patterns you would like to add to the defaults set in your Preferences.sublime-settings file. The plugin could then check if the "more" key exists, read both arrays, concatenate them, and write the result back to the .sublime-project file, erasing the "more" key at the same time. Finally, you could set up an event listener to run the plugin whenever you wanted - on save, upon loading a new file, etc.
EDIT
Here's a working example:
import sublime
import sublime_plugin
from copy import deepcopy
class ModifyExcludedFoldersCommand(sublime_plugin.WindowCommand):
def run(self):
proj_data = self.window.project_data() # dict
orig_proj_data = deepcopy(proj_data) # for comparison later
settings = sublime.load_settings("Preferences.sublime-settings")
fep = settings.get("folder_exclude_patterns") # list
for folder in proj_data["folders"]:
try:
if folder["folder_exclude_patterns"]:
break # if f_e_p is already present, our work is done
except KeyError:
pass # if it doesn't exist, move on to mfep
try:
mfep = folder["more_folder_exclude_patterns"]
new_fep = sorted(list(set(fep + mfep))) # combine f_e_p from
# Preferences and project,
# excluding duplicates using
# a set.
folder["folder_exclude_patterns"] = new_fep
del folder["more_folder_exclude_patterns"]
except KeyError:
pass # if mfep doesn't exist, just move on to the next folder
if proj_data != orig_proj_data:
self.window.set_project_data(proj_data)
class UpdateProjectData(sublime_plugin.EventListener):
def on_activated(self, view):
window = view.window()
window.run_command("modify_excluded_folders")
Save the file as Packages/User/modify_excluded_folders.py (where Packages is the folder opened when selecting Preferences -> Browse Packages...) and it should go into effect immediately. It will run each time a view is activated. It checks for the presence of a "folder_exclude_patterns" array in each folder defined in the current .sublime-project file, and if found it assumes everything is OK and passes on to the next folder. If that array is not found, it then checks for the presence of a "more_folder_exclude_patterns" array. If found, it does its magic and merges the contents with the existing "folder_exclude_patterns" array from your preferences (Default or User). It then writes a new "folder_exclude_patterns" array into the folder and deletes the "more_folder_exclude_patterns" array. Finally, it checks to see if any changes were made, and if so it writes the new data back to the .sublime-project file.

How do you delete or rename content parts in Orchard?

I gave a content part a wrong name and I want to rename it. I tried renaming the classes and all calls of it in the solution to no avail. I even tried deleting entries from the ShellFeatures table and the table generated by Migrations itself, but that just made things worse. Now the whole module's features aren't being recognized. Anyone tried this before?
I know this is an old question but I just had to tackle this and this is the first result I landed on from Google.
You can delete the ContentPart and add it again using migrations for example:
public int UpdateFrom3() {
// remove the AccreditationsPart part cleanly
ContentDefinitionManager.DeletePartDefinition(typeof (AccreditationsPart).Name);
enter code here
// re-add the AccreditationsPart again
ContentDefinitionManager.AlterPartDefinition(typeof (AccreditationsPart).Name,
cfg = > cfg
.Attachable()
.WithDescription("Adds a row of configurable accreditations to the content type"));
return 4;
}
You would need to customise the update numbers to fit your current migrations.cs and also change the AlterPartDefinition to match whatever you want it to be.
WARNING: This is only intended for fixing problems that you catch straight away. When you delete the part in the first section of the migration you will lose any associated data. If you already have the code running in production then you will have to use a different approach.

Resources