The Ctrl + Shift + F hotkey in Eclipse can format a file. It doesn't work for .json files. How to make it work?
You'll want to get the JSON Editor plugin if you don't already have it. You can find it here
The JSON Editor is a simple plugin for the Eclipse IDE that provides: - Color text highlighting - An Outline Tree view - JSON validation - Text formatting - Text folding for the JSON data format.
If the hot keys still don't work. Take a look under the menu as shown in the picture from their site here
Also, I see that there has been at least one issue with what looks to be the current versions formatting feature in the past.
From their discussion site:
rlespinola
2010-07-15 00:18:05 UTC
Using version 0.9.4, I do not see the option to "Format Text". Also, when I open a .json file, the outline view says "An outline is not available".
jdschulteis
2010-12-27 16:59:24 UTC
Using 0.9.4 on Helios, I also had "An outline is not available". I went to Window->Preferences->General->Editors->File Associations, selected '*.json' in the 'File types:' list, selected 'Json Editor' in the 'Associated editors:' list, and clicked 'Default'. I now get an outline, though it can be somewhat slow (6K line file).
Edit:
There are several ways to add a plugin. Eclipse recommends using the update manager.
Try help > software updates > find and install
You can look here for some alternative methods.
There are two options I figured out using Eclipse Luna (4.4.0).
Use a JSON Editor Plugin and define shortcuts
Download and install the JSON Editor Plugin from sourceforge manually or use the Eclipse marketplace (Help -> Eclipse marketplace) to find and install the plugin
Go to Window -> Preferences -> General -> Keys and filter for "format text".
Choose "Format Text" and set the "When:" value to "Editing Text" (sadly there is no explicit condition for JSON editing, but the format event for the JSON Editor is different to the other editors, so "Editing Text" will work aswell)
Set the "Binding:" to Ctrl + Shift + F
Use the Javascript Development Plugin with an ugly and anoying workaround
Get the plugin using Help -> Install New Software -> Work with: "http://download.eclipse.org/releases/luna" -> Programming Languages -> JavaScript Development Tools
Associate *.json files with the JavaScript Editor (Window -> Preferences -> General -> Editors -> File Associations)
You can now create files with "json" extension and edit them in Eclipse using the JavaScript Editor, but formatting with Ctrl + Shift + F will not work directly with the following unformatted example:
{"addressbook": {"name": "John Doe",
"address": {
"street": "5 Main Street", "city": "San Diego, CA", "zip": 91912
},
"phoneNumbers": [
"619 555-3452",
"664 555-4667"
]
}
}
The hack is to create a valid JavaScript variable from the object description like this:
var obj = {"addressbook": {"name": "John Doe",
"address": {
"street": "5 Main Street", "city": "San Diego, CA", "zip": 91912
},
"phoneNumbers": [
"619 555-3452",
"664 555-4667"
]
}
}
Using Ctrl + Shift + F will now work
In the end you will have to remove the "var obj =" hack to make the JSON file valid again
You could use the JavaScript editor that ships with Eclipse.
Related
I am using Visual Studio Code 1.60.1.
In my project I use many files called project.json and have created my custom JSON schema for them.
When I specify my schema via $schema field directly in project.json everything works just fine.
But I want to use .vscode/settings.json file for that:
{
"json.schemas": [
{
"fileMatch": [
"data/projects/list/*/project.json"
],
"url": "./.vscode/schemas/project.json"
}
]
}
This works too BUT in this case it appears that VS Code is mixing two schemas for my file, which is showing in a tip when I hover the opening { in editor:
JSON schema for .NET Core project.json files
[My scheme name]
This results in mixing properties and messed up hints and autocomplition suggestions.
How can I disable VS Code default ".NET Core" schema for my files and leave only my schema?
Turns out I had C# official extension installed.
I disabled it and everything is working just fine!
The moral is: if something goes wrong, don't forget to check your extensions!
I press ⌥ + ⇧ + F in Visual Studio Code for macOS, the shortcut to Format Document, to format a file called foo.rb or foo.html.erb.
Instead of formatting the document it prints out this letter: Ï
How do I get it to format the document?
You can set format associations in VSCode, so .erb files would be treated like .html.
Go to File->Preferences->Settings->Click ... in top right corner->Open settings.json
Then add this piece of code to your settings.json
"files.associations": {
"*.html.erb": "html"
}
This is how I solved this problem. It will remove some of the code highlights but will autoformat HTML templates like an HTML document.
You're going to need all of these settings in VS Code's settings.json file:
"ruby.rubocop.onSave": true,
"editor.formatOnSaveTimeout": 5000,
"editor.formatOnSave": true,
"files.associations": {
"*.erb": "erb"
},
Save the settings file. Install the "ruby-rubocop" and "ERB Formatter/Beautify" extensions on VS Code. Follow the documentation on both of those extensions to install their gem dependencies. Restart VS Code.
Format-on-save functionality will only trigger if the file is actually saved (which only happens if you change the file). Saving a file that has no changes will not trigger format-on-save.
If you're using prettier to format your html/css/js files, it is worth trying prettier-erb-plugin. Just add to your .prettierrc:
"plugins": ["#prettier/plugin-ruby", "prettier-plugin-erb"]
Or install it with yarn:
yarn add -D prettier #prettier/plugin-ruby prettier-plugin-erb
And make sure that VSCode uses local version of prettier from node_modules (or, you probably can install these plugins globally as well). Prettier VSCode plugin usually declared itself as default formatter, but just in case, make sure that in your settings.json is NOT written something like:
"[erb]": {
"editor.defaultFormatter": "aliariff.vscode-erb-beautify"
},
You can use Rufo to format your Ruby code. It is an opinionated formatter (like Prettier is for JS, if you are familiar with it).
You can use the vscode-rufo extension to integrate it with VSCode.
Update the settings.json of Visual Studio code:
File -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json"
Or in these paths in your OS
Windows %APPDATA%\Code\User\settings.json
macOS $HOME/Library/Application Support/Code/User/settings.json
Linux $HOME/.config/Code/User/settings.json
From Visual Studio Code Ruby extension documentation they recommend to use as an initial configuration:
"ruby.useBundler": true, //run non-lint commands with bundle exec
"ruby.useLanguageServer": true, // use the internal language server (see below)
"ruby.lint": {
"rubocop": {
"useBundler": true // enable rubocop via bundler
},
"reek": {
"useBundler": true // enable reek via bundler
}
},
"ruby.format": "rubocop" // use rubocop for formatting
Look at the linting documentation too for further improvements. Plus as mentioned previously, you can add that .erb should be treated as .html:
"files.associations": {
"*.html.erb": "html"
}
If you have no code formatting
That is, when you hit shift + option + F to format your code, vscode says something like:
install a formatter by clicking on the 'Extensions' tab on the left hand side of vscode, searching for 'ERB Formatter/Beautify' (by Ali Ariff), and installing it.
Run gem install htmlbeautifier
Press shift + command + P and search for
Preferences: Open Settings (JSON)
It should open a file that has a your JSON settings in it; something like this:
{
"window.zoomLevel": 1,
"editor.inlineSuggest.enabled": true
}
Add this to the settings.json file you opened in the previous step
"files.associations": {
"*.html.erb": "erb"
}
Your finished file might look like this:
{
"window.zoomLevel": 1,
"editor.inlineSuggest.enabled": true,
"files.associations": {
"*.html.erb": "erb"
}
}
Close and reopen vscode and it should now let you format with shift + option + F
If you have no syntax highlighting for erb files
The extension called 'ruby' will solve that.
Click on the 'Extensions' tab on the right hand side of vscode.
type in ruby
Install the ruby extension by Peng Lv
You may need to restart vscode
All done!
Reference
More info in this video
Nowadays (March 2019) I think prettier with prettier-ruby are the best options: it can handle Ruby, ERB (as HTML), JS, and many many more.
prettier script.rb # will show you the formatted script
prettier --write script.rb # will overwrite the file with the formatted script
You can use the Prettier VS Code plugin to do that automatically: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
https://github.com/prettier/plugin-ruby
gem install htmlbeautifier
through the search functionality provided in the editor with Ctrl +Shift+ P (or Command + Shift + P on Mac), and then searching for format document.
I use the rubocop instead of rufo.
At the beginning, I used rufo. However, I met the issue
{
boo: {
a: 1,
b: 2,
c: 3
}
}
it always format it for me as
{
boo: {
a: 1,
b: 2,
c: 3,
},
}
add two ,, behind c: 3 and boo: {}. It is that makes my rubocop fail always.
As for, I use the rubocop in the project. Why not use it format my codes directly!
If you are interested, you can do as the following:
install the plugin VSCode ruby and then add the following snippets in the the settings.json
"ruby.format": "rubocop",
"ruby.intellisense": "rubyLocate",
"ruby.useBundler": true,
"ruby.lint": {
"rubocop": {
"useBundler": true
}
},
save it. It works~~(I wish you)
It is now possible to:
Install ruby-rubocop in VS Code
Go to File -> Preferences -> Settings
Search for Editor: Default Formatter and select "misogi.ruby-rubocop"
Go to File -> Preferences -> Keyboard Shortcuts
Search for Ruby: autocorrect by rubocop. There you have the shortcut to run rubocop in order to automatic format your ruby code following your rubocop settings.
You may also right click in your ruby file and you will find the "Format Document" option, which triggers "Ruby: autocorrect by rubocop" once ruby-rubocop is installed.
To format your ruby files, you don't need any extra plugin, you can just map some keys to do "editor.action.reindentLines"
If you use vscode-vim plugin, you can add this to your settings:
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["=", "="],
"commands": ["editor.action.reindentlines"]
}
],
Then in normal vim mode, == will reformat your file.
TL;DR: SourceTree for Windows recently added the "commit text links" feature, but it appears that the replacements must be set up per-repository. Is there a way to apply them globally or a config file that could be modified programmatically to set them?
Long version: The "commit text links" feature looks incredibly useful but I have a bit of a problem: We have about a dozen JIRA projects and over 25 repositories that each of them could be related to (none of them are 1-to-1 mappings). While I could set up a single regular expression to match each of the JIRA projects, it's a bit much to ask all of my developers to set it up through the UI for each and every repository. To really take advantage of this I ideally need to be able to give them instructions on a single file to modify or I need to generate a setup script that I can distribute to our developers.
Is there a config file that this setting is saved in? I was expecting to see it in something like .hg/hgrc but I couldn't find anything. I also couldn't find any relevant settings in the SourceTree Program Files folder.
Alternatively, is there a global or default setting that can be applied across all repositories? That plus the regex version could make setup significantly less painful if still manual.
Thanks!
(Note: I'm in version 1.3.3.0 of SourceTree for Windows, which I believe is the most recent stable version)
May be a bit late, but I've found a relatively easy way to do this.
Underneath your .hg/.git folder within your repository should exist a file called 'sourcetreeconfig.' This is where the links live and can be manually edited.
First make sure that you have closed all of the existing repository tabs within sourcetree, and additionally close sourcetree afterwards. Then, (assuming you have already configured a repository) copy the block from the respective repository's sourcetreeconfig and do a replace across all of your sourcetreeconfig files. This would be if you have multiple tied to the same project. It should be relatively easy to throw something together that can configure for different projects, just replace the url/project within the config.
Upon reopening sourcetree, each of your repositories should reflect this change.
This was performed using version 1.6.5.0 of sourcetree.
Programmatic Solution
Here in late 2019, the ability to globally configure commit text links in Sourcetree 3.2.6 for Windows still does not exist. Since this question was one of the few hits with a decent answer, I figured I would add an automated solution to the answers. I'm not a programmer, and I know the RegEx isn't the best, but this simple PowerShell script I cobbled together gets the job done. Make sure Sourcetree is closed before you run the script.
Copy the sourcelinker script into a Notepad++ or similar text editor application.
In order get the specific string for your setup, configure one of your Git repos with one or more commit text links specific to your organization.
A. Launch Sourcetree, select a Git repo, and click Settings.
B. In the Repository Settings window, click the Advanced tab.
C. In the Commit text links area, click Add.
D. From the Replacement type drop-down list, select Other.
E. Enter the Regex pattern and Link to URL for your specific setup, and click OK.
F. In the Repository Settings window, click OK.
G. Close Sourcetree.
Navigate to the .git sub-directory of the repo you configured, and open sourcetreesonfig.json.
Copy everything starting with "CommitTextLinks": [ through the closing bracket and comma ],. For example:
"CommitTextLinks": [
{
"$id": "11",
"LinkType": 99,
"Regex": "[fF][bB][#\\s]*(\\d+)",
"LinkToUrl": "https://companyname.fogbugz.com/f/cases/$1",
"Project": null,
"RootUrl": null,
"Description": "[fF][bB][#\\s]*(\\d+) => https://companyname.fogbugz.com/f/cases/$1"
}
],
Paste the copied content into your sourcelinker script between the single quotes that follow $New1 =.
Save the script as sourcelinker.ps1.
Copy sourcelinker.ps1 to the root folder where your Git repos reside.
Right-click the script, and select Run with PowerShell.
Launch Sourcetree, and check the Commit text links for your other Git repos.
Sourcelinker script
This script example contains Regex examples that link to Fogbugz and handles variations such as:
case12345
fb12345
bugzid12
Script
# Sourcelinker script
$InputFiles = Get-Item ".\*\.git\sourcetreeconfig.json"
$Old1 = '"CommitTextLinks": null,'
$New1 = '"CommitTextLinks": [
{
"$id": "9",
"LinkType": 99,
"Regex": "[bB][Uu][gG][sSzZ]\\s*[Ii][Dd]s?\\s*[#:; ]+(\\d+)",
"LinkToUrl": "https://companyname.fogbugz.com/f/cases/$1",
"Project": null,
"RootUrl": null,
"Description": "[bB][Uu][gG][sSzZ]\\s*[Ii][Dd]s?\\s*[#:; ]+(\\d+) => https://companyname.fogbugz.com/f/cases/$1"
},
{
"$id": "10",
"LinkType": 99,
"Regex": "[cC][aA][Ss][Ee]+\\s*(\\d+)",
"LinkToUrl": "https://companyname.fogbugz.com/f/cases/$1",
"Project": null,
"RootUrl": null,
"Description": "[cC][aA][Ss][Ee]+\\s*(\\d+) => https://companyname.fogbugz.com/f/cases/$1"
},
{
"$id": "11",
"LinkType": 99,
"Regex": "[fF][bB][#\\s]*(\\d+)",
"LinkToUrl": "https://companyname.fogbugz.com/f/cases/$1",
"Project": null,
"RootUrl": null,
"Description": "[fF][bB][#\\s]*(\\d+) => https://companyname.fogbugz.com/f/cases/$1"
}
],'
$InputFiles | ForEach {
(Get-Content -Path $_.FullName).Replace($Old1,$New1) | Set-Content -Path $_.Fullname
}
Solution inspired thanks to suggestion by Andrew Pearce from this thread.
I'm developing an add-on for Firefox and I now wish to localize the name and description of the add-on itself, as visible to the user in the Add-ons Manager menu.
However, the guides I've seen online only seem to mention preferences in package.json and nothing about the name of the add-on or its description. I have tried to apply the suggestions for preferences to the name/description fields and haven't had any success - it will always appear in English.
Is localization of these fields even possible?
There is currently no way to do this from package.json. Read bug 661083 for more details.
However, there is a workaround: Manually edit install.rdf to add em:localized properties.
To do this you'll need to use the SDK to package your app into an xpi file. Then open the xpi (it is a zip file) and you'll see install.rdf in the root of the directory.
The MDN article Localizing extension descriptions describes what structure the em:localized properties needs to have.
This functionality has been added to the jpm tool recently (February 2016, see issue 495). Make sure that you use a recent jpm version, then the following code in package.json will work:
"title": "Default add-on name",
"description": "Default add-on description",
"locales": {
"de": {
"title": "Add-on name in German",
"description": "Add-on description in German"
},
"ru": {
"title": "Add-on name in Russian",
"description": "Add-on description in Russian"
},
}
I need Notepad++ to take a json string from this
{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}
to this...
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
I looked around at all the TextFX options but couldn't find anything that worked.
Update:
As of Notepad++ v7.6, use Plugin Admin to install JSTool per this answer
INSTALL
Download it from http://sourceforge.net/projects/jsminnpp/ and copy JSMinNpp.dll to plugin directory of Notepad++. Or you can just install "JSTool" from Plugin Manager in Notepad++.
New Notepad++ install and where did PluginManager go? See How to view Plugin Manager in Notepad++
{
"menu" : {
"id" : "file",
"value" : "File",
"popup" : {
"menuitem" : [{
"value" : "New",
"onclick" : "CreateNewDoc()"
}, {
"value" : "Open",
"onclick" : "OpenDoc()"
}, {
"value" : "Close",
"onclick" : "CloseDoc()"
}
]
}
}
}
Tip: Select the code you want to reformat, then Plugins | JSTool | JSFormat.
For Notepad++ v.7.6 and above Plugins Admin... is available.
Open Menu Plugins > Plugins Admin...
Search JSON Viewer
Check JSON Viewer in List
Click on Install Button
Restart Notepad++
Select JSON text
Go to Plugins > JSON Viewer > Format JSON ( Ctrl + Alt + Shift + M )
We can install any Notepad++ supported plugins using Plugins Admin...
You require a plugin to format JSON. To install the plugin do the following steps:
Open notepad++ -> ALT+P -> Plugin Manager -> Selcet JSON Viewer -> Click
Install
Restart notepad++
Now you can use shortcut to format json as CTRL + ALT +SHIFT + M
or ALT+P -> Plugin Manager -> JSON Viewer -> Format JSON
I personally use JSON Viewer since the Notepad++ plugin doesn't work any more.
EDIT - 24th May 2012
I advise that you download the JSMin plugin for Notepad as mentioned in the answer. This works well for me in the latest version (v6.1.2 at time of writing).
EDIT - 7th November 2017
As per #danday74's comment below, JSMin is now JSToolNpp. Also, please be aware that the JSON Viewer tool is on Codeplex which will likely disappear in the near future.
Given the above, this answer is no longer relevant and you should use Dan H's answer instead. My answer is simply here for posterity.
Universal Indent GUI plugin for Notepad++ will turn your sample into:
{
"menu" : {
"id" : "file", "value" : "File", "popup" : {
"menuitem" : [ {
"value" : "New", "onclick" : "CreateNewDoc()";
}
, {
"value" : "Open", "onclick" : "OpenDoc()";
}
, {
"value" : "Close", "onclick" : "CloseDoc()";
}
];
}
}
}
As per the latest notepad++, updated answer. Install JSON Viewer
Open notepad++ go to Plugins --> click Plugins Admin..
In Plugins Admin window search for JSON Viewer and click on Install
New after installing plugin, this is how you can view file in JSON format
simply go to this link
download the dll
copy and paste the dll to the plugins folder at notepad++, \Notepad++\plugins
restart the notepad++, and it should be shown in the list
NOTE: this dll supports 64 bit notepade++
Steps to add JSON viewer plugin for notepad++:
Download JSON viewer plugin for notepad++ from sourceforge.
Copy NPPJSONViewer.dll into Notepad++\plugins\NPPJSONViewer folder
restart the notepad++
select JSON and click on plugins > JSON Viewer > Format JSON (CTRL+ALT+SHIFT+M).
Done.
It's not an NPP solution, but in a pinch, you can use this online JSON Formatter and then just paste the formatted text into NPP and then select Javascript as the language.
If formatting JSON is the main goal and you have VisualStudio then it is simple and easy.
Open Visual Studio
File -> New -> File
Select Web in left side panel
Select JSON
Copy paste your raw JSON value
Press Ctrl + K and Ctrl + D
That's it. you will get formatted JSON Value.
JSMinNpp plugin will do this job.
https://sourceforge.net/projects/jsminnpp/
The following Notepad++ plugin worked for me as suggested by "SUN"
https://sourceforge.net/projects/jsminnpp/
You can view in Notepad++ no problem now (maybe older versions were bugged?)
for win64:
You can find the latest plugin here: https://github.com/kapilratnani/JSON-Viewer/releases . The latest zip file contains a .dll file.
And then follow the github priject README instructions:
Paste the file "NPPJSONViewer.dll" to Notepad++ plugin folder
open a document containing a JSON string
Select JSON fragment and navigate to plugins/JSON Viewer/show JSON Viewer or press "Ctrl+Alt+Shift+J"
Voila!! if the JSON is valid, it will be shown in a Treeview
It should be the same process for win32 but I cannot personally verify it.
If you don't want to install a Notepad++ plugin but you have Firefox and a JSON plugin for Firefox, you can select Run -> Launch in Firefox. You get the contents formatted as JSON using your Firefox plugin.
This is what I personally do.
It worked for me in the latest edition to Notepad using the UniversalIndentGui.
What I did was under the plugin setting choose Enable Text Auto Update, a window popped up and I selected javascript.
Notepad 5.8.7 and jsmin 1.7.0.0 works wonderful here.
Be careful though, found out jsmin eats the comments the hard way (should have read first).
I'm using the JSON Viewer plug-in with NPP 5.9 and it seems to work well.
I know this thread is old but I recently ran into a problem with JSToolNPP not being compatible with my newly updated N++, I did find a replacement that seems to work. http://sourceforge.net/projects/nppjsonviewer/
Use at your own risk, ofc. (standard disclaimer from me when linking anything outside the SExchange, fyi)
For those of us behind a corporate firewall with no direct access to the internet, using the Plugins Admin won't work. To use the JSMinNpp plugin, you can't just "copy the dll to the plugins folder". It needs to live inside a folder called "JSMinNpp" inside the plugins folder. After doing that and restarting Notepad++, I was able to see the "JSTool" menu option under the Plugins menu.
Tested with Notepad++ 7.8.2 and 7.8.9.
I use 32-bit Notepad++ version 7.5.6. I have found that 32-bit JSToolNpp 1.20.0
does a great job.
The direct link is:
https://sourceforge.net/projects/jsminnpp/files/Uni/JSToolNPP.1.2006.0.uni.32.zip/download
which is redirected from https://sourceforge.net/projects/jsminnpp/.
VirusTotal link:
https://www.virustotal.com/gui/file/008ee0ce889dfd9e96b975cebe6faafe28bc350352e951f3dec97e8e5bec5a07
JSON-Viewer
works fine too, but cannot sort the JSON data.
You can use http://www.jsonlint.com/ to edit your json online if you don't have Notepad++.
Your best bet is to use one of the latest versions of Eclipse (I am using Eclipse Galileo J2EE and Eclipse Ganymede J2EE). Create a JavaScript file, then create a variable:
var jsonObject = {"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}};
Lastly, hit CTRL+SHIFT+F and voila! You have a nicely indented JSON Object. I, too, am looking for a Notepad++ JSON formatter, and I very well may be forced to develop an Npp plugin some short time in the future.
I know you asked about NotePad++ but TextMate for OS X can do it via the JSON bundle, its called the "Reformat Document" command.