Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
vscode-version: ['1.72.0', '1.100.0']
vscode-version: ['1.90.0', '1.104.0']
fail-fast: false

runs-on: ${{ matrix.os }}
Expand Down
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
- Record the whole window with the sidebar hidden
- Resize gif with `gifsicle --resize 800x sortOrderExample.gif > sortOrderExample.gif`

# Debug extension tests
You can change `test` to `test.only` to execute only a single test

## Current minimally supported VSCode version
I only increase the minimally supported code version, if there is feature in a newer version that I need. The most current feature that is in use is `--profile-temp` which was introduced in [1.72](https://code.visualstudio.com/updates/v1_72#_extension-debugging-in-a-clean-environment)
Currently, the version is set at 1.190, because the tests in the pipeline failed for lower versions and it wasn't reproducible otherwise. Usually, I only increase the minimally supported code version, if there is feature in a newer version that I need.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Sort JSON/JS array
# Sort JSON/JSONL/JSONC/JS array
![test status badge](https://github.com/fvclaus/vsc-sort-json-array/actions/workflows/ci.yml/badge.svg?branch=master)
<a href="https://marketplace.visualstudio.com/items?itemName=fvclaus.sort-json-array">
<img alt="VS Code Marketplace Installs" src="https://img.shields.io/visual-studio-marketplace/i/fvclaus.sort-json-array"></a>

Sorts a JSON/JS array by common property or by custom function and replace the array in-place. The array can be selected (must include `[` and `]`). If no selection is present, the extension will try to find an array that is enclosed by the current cursor position.
Sorts a JSON/JSONL/JSONC/JS array by common property or by custom function and replace the array in-place. The array can be selected (must include `[` and `]`). If no selection is present, the extension will try to find an array that is enclosed by the current cursor position.

## Installation

Install through VS Code extensions. Search for `sort js array`

[Visual Studio Code Market Place: Sort JSON/JS array](https://marketplace.visualstudio.com/items?itemName=fvclaus.sort-json-array)
[Visual Studio Code Market Place: Sort JSON/JSONL/JSONC/JS array](https://marketplace.visualstudio.com/items?itemName=fvclaus.sort-json-array)

Can also be installed in VS Code: Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.

Expand All @@ -19,7 +19,7 @@ ext install fvclaus.sort-json-array

## What is supported?

This extension contains its own parser, because `JSON.parse` is too restrictive and `eval` doesn't support comments (which is an upcoming feature) and will strip out some information (for example how and if object properties are quoted).
This extension contains its own parser, because `JSON.parse` is too restrictive and `eval` doesn't support comments and will strip out some information (for example how and if object properties are quoted). The result is a support for a superset of JSON/JSONC/JSONL and a subset of JS.

- Arrays:
- `number[]`, `string[]` or `object[]`
Expand All @@ -28,7 +28,11 @@ This extension contains its own parser, because `JSON.parse` is too restrictive
- Object properties/key can be either unquoted, `'single'` or `"double"` quoted
- Strings: `'single'` and `"double"` quoted
- Numbers: Base 10 numbers with optional exponent
- Comments:
- `// Line comments`
- `/* Block comments stretching multiple lines`
- JSONL: with the same rules as above. Selection of an arbitrary number of lines is supported.
- JSONC: Full support with the same rules as above

## Demo

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "sort-json-array",
"displayName": "Sort JSON/JS array",
"description": "Sorts JSON/JS arrays by value, object property or custom function",
"displayName": "Sort JSON/JSONL/JSONC/JS array",
"description": "Sorts JSON/JSONL/JSONC/JS arrays by value, object property or custom function",
"version": "4.1.0",
"publisher": "fvclaus",
"license": "BSD-3-Clause",
"engines": {
"vscode": "^1.44.0",
"vscode": "^1.90.0",
"node": ">=22.0.0"
},
"bugs": {
Expand Down Expand Up @@ -36,17 +36,17 @@
"commands": [
{
"command": "extension.sortJsonArrayAscending",
"title": "Sort JSON/JS array ascending",
"title": "Sort JSON/JSONL/JSONC/JS array ascending",
"enablement": "editorLangId == json || editorLangId == jsonc || editorLangId == jsonl || editorLangId == typescript || editorLangId == typescriptreact || editorLangId == javascript || editorLangId == javascriptreact || editorLangId == dart || editorLangId == coffeescript"
},
{
"command": "extension.sortJsonArrayDescending",
"title": "Sort JSON/JS array descending",
"title": "Sort JSON/JSONL/JSONC/JS array descending",
"enablement": "editorLangId == json || editorLangId == jsonc || editorLangId == jsonl || editorLangId == typescript || editorLangId == typescriptreact || editorLangId == javascript || editorLangId == javascriptreact || editorLangId == dart || editorLangId == coffeescript"
},
{
"command": "extension.sortJsonArrayCustom",
"title": "Sort JSON/JS array custom",
"title": "Sort JSON/JSONL/JSONC/JS array custom",
"enablement": "editorLangId == json || editorLangId == jsonc || editorLangId == jsonl || editorLangId == typescript || editorLangId == typescriptreact || editorLangId == javascript || editorLangId == javascriptreact || editorLangId == dart || editorLangId == coffeescript"
}
],
Expand Down
3 changes: 3 additions & 0 deletions src/parser/JSON.g4
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,8 @@ IDENTIFIER
// Single-line comments
LINE_COMMENT : '//' ~[\r\n]* -> channel(HIDDEN);

// Multi-line comments
BLOCK_COMMENT : '/*' .*? '*/' -> channel(HIDDEN);

// handle characters which failed to match any other token
ErrorCharacter : . ;
4 changes: 3 additions & 1 deletion src/parser/generated/JSON.interp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ null
null
null
null
null

token symbolic names:
null
Expand All @@ -34,6 +35,7 @@ NUMBER
WS
IDENTIFIER
LINE_COMMENT
BLOCK_COMMENT
ErrorCharacter

rule names:
Expand All @@ -45,4 +47,4 @@ arr


atn:
[3, 51485, 51898, 1421, 44986, 20307, 1543, 60043, 49729, 3, 18, 66, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 24, 10, 3, 3, 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, 37, 3, 2, 2, 2, 37, 38, 7, 10, 2, 2, 38, 42, 3, 2, 2, 2, 39, 40, 7, 11, 2, 2, 40, 42, 7, 10, 2, 2, 41, 25, 3, 2, 2, 2, 41, 39, 3, 2, 2, 2, 42, 7, 3, 2, 2, 2, 43, 44, 9, 2, 2, 2, 44, 45, 7, 12, 2, 2, 45, 46, 5, 4, 3, 2, 46, 9, 3, 2, 2, 2, 47, 48, 7, 8, 2, 2, 48, 53, 5, 4, 3, 2, 49, 50, 7, 7, 2, 2, 50, 52, 5, 4, 3, 2, 51, 49, 3, 2, 2, 2, 52, 55, 3, 2, 2, 2, 53, 51, 3, 2, 2, 2, 53, 54, 3, 2, 2, 2, 54, 57, 3, 2, 2, 2, 55, 53, 3, 2, 2, 2, 56, 58, 7, 7, 2, 2, 57, 56, 3, 2, 2, 2, 57, 58, 3, 2, 2, 2, 58, 59, 3, 2, 2, 2, 59, 60, 7, 9, 2, 2, 60, 64, 3, 2, 2, 2, 61, 62, 7, 8, 2, 2, 62, 64, 7, 9, 2, 2, 63, 47, 3, 2, 2, 2, 63, 61, 3, 2, 2, 2, 64, 11, 3, 2, 2, 2, 9, 23, 31, 35, 41, 53, 57, 63]
[3, 51485, 51898, 1421, 44986, 20307, 1543, 60043, 49729, 3, 19, 66, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 24, 10, 3, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 30, 10, 4, 12, 4, 14, 4, 33, 11, 4, 3, 4, 5, 4, 36, 10, 4, 3, 4, 3, 4, 3, 4, 3, 4, 5, 4, 42, 10, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 7, 6, 52, 10, 6, 12, 6, 14, 6, 55, 11, 6, 3, 6, 5, 6, 58, 10, 6, 3, 6, 3, 6, 3, 6, 3, 6, 5, 6, 64, 10, 6, 3, 6, 2, 2, 2, 7, 2, 2, 4, 2, 6, 2, 8, 2, 10, 2, 2, 3, 4, 2, 13, 13, 16, 16, 2, 73, 2, 12, 3, 2, 2, 2, 4, 23, 3, 2, 2, 2, 6, 41, 3, 2, 2, 2, 8, 43, 3, 2, 2, 2, 10, 63, 3, 2, 2, 2, 12, 13, 5, 10, 6, 2, 13, 14, 7, 2, 2, 3, 14, 3, 3, 2, 2, 2, 15, 24, 5, 6, 4, 2, 16, 24, 5, 10, 6, 2, 17, 24, 7, 13, 2, 2, 18, 24, 7, 14, 2, 2, 19, 24, 7, 3, 2, 2, 20, 24, 7, 4, 2, 2, 21, 24, 7, 5, 2, 2, 22, 24, 7, 6, 2, 2, 23, 15, 3, 2, 2, 2, 23, 16, 3, 2, 2, 2, 23, 17, 3, 2, 2, 2, 23, 18, 3, 2, 2, 2, 23, 19, 3, 2, 2, 2, 23, 20, 3, 2, 2, 2, 23, 21, 3, 2, 2, 2, 23, 22, 3, 2, 2, 2, 24, 5, 3, 2, 2, 2, 25, 26, 7, 11, 2, 2, 26, 31, 5, 8, 5, 2, 27, 28, 7, 7, 2, 2, 28, 30, 5, 8, 5, 2, 29, 27, 3, 2, 2, 2, 30, 33, 3, 2, 2, 2, 31, 29, 3, 2, 2, 2, 31, 32, 3, 2, 2, 2, 32, 35, 3, 2, 2, 2, 33, 31, 3, 2, 2, 2, 34, 36, 7, 7, 2, 2, 35, 34, 3, 2, 2, 2, 35, 36, 3, 2, 2, 2, 36, 37, 3, 2, 2, 2, 37, 38, 7, 10, 2, 2, 38, 42, 3, 2, 2, 2, 39, 40, 7, 11, 2, 2, 40, 42, 7, 10, 2, 2, 41, 25, 3, 2, 2, 2, 41, 39, 3, 2, 2, 2, 42, 7, 3, 2, 2, 2, 43, 44, 9, 2, 2, 2, 44, 45, 7, 12, 2, 2, 45, 46, 5, 4, 3, 2, 46, 9, 3, 2, 2, 2, 47, 48, 7, 8, 2, 2, 48, 53, 5, 4, 3, 2, 49, 50, 7, 7, 2, 2, 50, 52, 5, 4, 3, 2, 51, 49, 3, 2, 2, 2, 52, 55, 3, 2, 2, 2, 53, 51, 3, 2, 2, 2, 53, 54, 3, 2, 2, 2, 54, 57, 3, 2, 2, 2, 55, 53, 3, 2, 2, 2, 56, 58, 7, 7, 2, 2, 57, 56, 3, 2, 2, 2, 57, 58, 3, 2, 2, 2, 58, 59, 3, 2, 2, 2, 59, 60, 7, 9, 2, 2, 60, 64, 3, 2, 2, 2, 61, 62, 7, 8, 2, 2, 62, 64, 7, 9, 2, 2, 63, 47, 3, 2, 2, 2, 63, 61, 3, 2, 2, 2, 64, 11, 3, 2, 2, 2, 9, 23, 31, 35, 41, 53, 57, 63]
3 changes: 2 additions & 1 deletion src/parser/generated/JSON.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ NUMBER=12
WS=13
IDENTIFIER=14
LINE_COMMENT=15
ErrorCharacter=16
BLOCK_COMMENT=16
ErrorCharacter=17
'true'=1
'false'=2
'null'=3
Expand Down
5 changes: 4 additions & 1 deletion src/parser/generated/JSONLexer.interp

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/parser/generated/JSONLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ NUMBER=12
WS=13
IDENTIFIER=14
LINE_COMMENT=15
ErrorCharacter=16
BLOCK_COMMENT=16
ErrorCharacter=17
'true'=1
'false'=2
'null'=3
Expand Down
Loading