Skip to content

Commit

Permalink
feat: include file name setting
Browse files Browse the repository at this point in the history
idea from @justinMBullard fork
  • Loading branch information
MaloPolese committed Apr 23, 2023
1 parent f26f4f5 commit 2d3fa98
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ This extension allows you to copy and paste a jsonpath
If you want to use bracket notation instead of dot notation, you can set the `copy-json-path.bracketNotation` setting to `true`.

![Bracket notation setting](assets/settings-bracket-notation.png 'bracket notation')

## File name

If you want to add the file name at the beginning of the path, you can set the `copy-json-path.includeFileName` setting to `true`.

![File name setting](assets/settings-file-name.png 'file name')
Binary file added assets/settings-file-name.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"configuration": {
"title": "Copy Json Path",
"properties": {
"json.copyJsonPath.includeFileName": {
"type": "boolean",
"default": false,
"description": "Include the name of the json file at the beginning of the path"
},
"json.copyJsonPath.useBracketNotation": {
"type": "boolean",
"default": false,
Expand Down
4 changes: 3 additions & 1 deletion src/commands/copy.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ export class Copy {

const configuration = workspace.getConfiguration('json.copyJsonPath');

const includeFileName = configuration.get<boolean>('includeFileName');
const useBracketNotation = configuration.get<boolean>('useBracketNotation');

if (offset && text) {
let path: string = getJsonPath(text, offset, {
let path: string = getJsonPath(text, offset, editor, {
includeFileName,
useBracketNotation,
});

Expand Down
18 changes: 17 additions & 1 deletion src/json.path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ import * as jsonc from 'jsonc-parser';
import { TextEditor } from 'vscode';

interface Options {
includeFileName?: boolean;
useBracketNotation?: boolean;
}

export default function getJsonPath(
jsonText: string,
offsetPosition: number,
editor: TextEditor | undefined,
options?: Options,
) {
const location = jsonc.getLocation(jsonText, offsetPosition);

const path: string = location.path.reduce(
const locationPath = location.path;
if (options?.includeFileName) {
includeTitle(locationPath, editor);
}

const path: string = locationPath.reduce(
(
accumulated: string,
propertyName: jsonc.Segment,
Expand All @@ -30,6 +37,15 @@ export default function getJsonPath(
return path;
}

function includeTitle(path: jsonc.JSONPath, editor: TextEditor | undefined) {
const fileName = editor?.document.fileName.split('/').pop()?.split('.')[0];

if (fileName) {
const a: jsonc.Segment = fileName;
path.unshift(fileName);
}
}

function getPropertyPath(
propertyName: jsonc.Segment,
isFirst: boolean,
Expand Down
4 changes: 2 additions & 2 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ suite('Extension Test Suite', () => {
offsetPosition: number,
expectedPropertyPath: any,
) {
var jsonPath = getJsonPath(jsonText, offsetPosition);
var jsonPath = getJsonPath(jsonText, offsetPosition, undefined);
assert.strictEqual(
jsonPath,
expectedPropertyPath,
Expand All @@ -120,7 +120,7 @@ suite('Extension Test Suite', () => {
offsetPosition: number,
expectedValue: any,
) {
var jsonPath = getJsonPath(jsonText, offsetPosition);
var jsonPath = getJsonPath(jsonText, offsetPosition, undefined);
var parsedJson = JSON.parse(jsonText);
const actualValue = eval('parsedJson' + jsonPath);
assert.strictEqual(
Expand Down

0 comments on commit 2d3fa98

Please sign in to comment.