Skip to content

Commit

Permalink
feat: ✨ add transform controller and enhance localization for convers…
Browse files Browse the repository at this point in the history
…ion features
  • Loading branch information
ManuelGil committed Jan 15, 2025
1 parent a17b8f6 commit 7b01a65
Show file tree
Hide file tree
Showing 12 changed files with 1,086 additions and 613 deletions.
892 changes: 465 additions & 427 deletions package.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.nls.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"jsonFlow.files.openFile": "Datei öffnen",
"jsonFlow.files.convertToJson": "In JSON konvertieren",
"jsonFlow.files.convertPartialToJson": "Auswahl in JSON konvertieren",
"jsonFlow.files.convertToType": "In Typ oder Struktur umwandeln",
"jsonFlow.files.convertPartialToType": "Auswahl in Typ oder Struktur umwandeln",
"jsonFlow.files.copyContent": "Inhalt in Zwischenablage kopieren",
"jsonFlow.files.copyContentAsJson": "Inhalt als JSON kopieren",
"jsonFlow.files.copyContentPartialAsJson": "Auswahl als JSON kopieren",
Expand Down
2 changes: 2 additions & 0 deletions package.nls.es.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"jsonFlow.files.openFile": "Abrir Archivo",
"jsonFlow.files.convertToJson": "Convertir a JSON",
"jsonFlow.files.convertPartialToJson": "Convertir Selección a JSON",
"jsonFlow.files.convertToType": "Convertir a Tipo o Estructura",
"jsonFlow.files.convertPartialToType": "Convertir Selección a Tipo o Estructura",
"jsonFlow.files.copyContent": "Copiar Contenido al Portapapeles",
"jsonFlow.files.copyContentAsJson": "Copiar Contenido como JSON",
"jsonFlow.files.copyContentPartialAsJson": "Copiar Selección como JSON",
Expand Down
2 changes: 2 additions & 0 deletions package.nls.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"jsonFlow.files.openFile": "Ouvrir le fichier",
"jsonFlow.files.convertToJson": "Convertir en JSON",
"jsonFlow.files.convertPartialToJson": "Convertir la sélection en JSON",
"jsonFlow.files.convertToType": "Convertir en type ou structure",
"jsonFlow.files.convertPartialToType": "Convertir la sélection en type ou structure",
"jsonFlow.files.copyContent": "Copier le contenu dans le presse-papiers",
"jsonFlow.files.copyContentAsJson": "Copier le contenu en tant que JSON",
"jsonFlow.files.copyContentPartialAsJson": "Copier la sélection en tant que JSON",
Expand Down
2 changes: 2 additions & 0 deletions package.nls.it.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"jsonFlow.files.openFile": "Apri File",
"jsonFlow.files.convertToJson": "Converti in JSON",
"jsonFlow.files.convertPartialToJson": "Converti Selezione in JSON",
"jsonFlow.files.convertToType": "Converti in Tipo o Struttura",
"jsonFlow.files.convertPartialToType": "Converti Selezione in Tipo o Struttura",
"jsonFlow.files.copyContent": "Copia Contenuto negli Appunti",
"jsonFlow.files.copyContentAsJson": "Copia Contenuto come JSON",
"jsonFlow.files.copyContentPartialAsJson": "Copia Selezione come JSON",
Expand Down
2 changes: 2 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"jsonFlow.files.openFile": "Open File",
"jsonFlow.files.convertToJson": "Convert to JSON",
"jsonFlow.files.convertPartialToJson": "Convert Selection to JSON",
"jsonFlow.files.convertToType": "Convert to Type or Structure",
"jsonFlow.files.convertPartialToType": "Convert Selection to Type or Structure",
"jsonFlow.files.copyContent": "Copy Content to Clipboard",
"jsonFlow.files.copyContentAsJson": "Copy Content as JSON",
"jsonFlow.files.copyContentPartialAsJson": "Copy Selection as JSON",
Expand Down
2 changes: 2 additions & 0 deletions package.nls.pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"jsonFlow.files.openFile": "Abrir Arquivo",
"jsonFlow.files.convertToJson": "Converter para JSON",
"jsonFlow.files.convertPartialToJson": "Converter Seleção para JSON",
"jsonFlow.files.convertToType": "Converter para Tipo ou Estrutura",
"jsonFlow.files.convertPartialToType": "Converter Seleção para Tipo ou Estrutura",
"jsonFlow.files.copyContent": "Copiar Conteúdo para a Área de Transferência",
"jsonFlow.files.copyContentAsJson": "Copiar Conteúdo como JSON",
"jsonFlow.files.copyContentPartialAsJson": "Copiar Seleção como JSON",
Expand Down
149 changes: 1 addition & 148 deletions src/app/controllers/files.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class FilesController {
arguments: [document.uri],
},
document.uri,
'file',
document.fileName,
),
);
}
Expand Down Expand Up @@ -132,153 +132,6 @@ export class FilesController {
}
}

/**
* The convertToJson method.
*
* @function convertToJson
* @param {NodeModel | Uri} node - The node model
* @public
* @memberof FilesController
* @example
* controller.convertToJson('file:///path/to/file');
*
* @returns {void} - The promise
*/
convertToJson(node: NodeModel | Uri) {
if (node) {
// Get the resource URI
const resourceUri = node instanceof NodeModel ? node.resourceUri : node;

// Check if the resource URI is valid
if (!resourceUri) {
return;
}

// Open the text document
workspace.openTextDocument(resourceUri).then(async (document) => {
// Get the language ID and file name
const { languageId, fileName } = document;

// Determine the file type, defaulting to 'json' if unsupported
let fileType = languageId;

if (!isFileTypeSupported(fileType)) {
const fileExtension = fileName.split('.').pop();

fileType = fileExtension;
}

// Parse JSON content
const jsonContent = parseJSONContent(
document.getText(),
fileType as FileType,
);

// Check if the content is null
if (jsonContent === null) {
return;
}

// Open the JSON document
const jsonDocument = await workspace.openTextDocument({
language: 'json',
content: JSON.stringify(jsonContent, null, 2),
});

// Show the JSON document
window.showTextDocument(jsonDocument);
});
}
}

/**
* The convertPartialToJson method.
*
* @function convertPartialToJson
* @public
* @memberof FilesController
* @example
* controller.convertPartialToJson();
*
* @returns {void} - The promise
*/
async convertPartialToJson() {
// Get the active text editor
const editor = window.activeTextEditor;

// Check if there is an active editor
if (!editor) {
const message = l10n.t('No active editor!');
window.showErrorMessage(message);
return;
}

// Check if there is a selection
const selection = editor.selection;

if (selection.isEmpty) {
const message = l10n.t('No selection!');
window.showErrorMessage(message);
return;
}

// Get the selection range
const selectionRange = new Range(
selection.start.line,
selection.start.character,
selection.end.line,
selection.end.character,
);

// Get the language ID and file name
const { languageId, fileName } = editor.document;

let fileType = languageId;

let text = editor.document.getText(selectionRange);

if (
[
'javascript',
'javascriptreact',
'typescript',
'typescriptreact',
].includes(fileType)
) {
fileType = 'jsonc';

text = text
.replace(/'([^']+)'/g, '"$1"')
.replace(/(['"])?([a-zA-Z0-9_]+)(['"])?:/g, '"$2":')
.replace(/,*\s*\n*\]/g, ']')
.replace(/{\s*\n*/g, '{')
.replace(/,*\s*\n*};*/g, '}');
}

if (!isFileTypeSupported(fileType)) {
const fileExtension = fileName.split('.').pop();

fileType = isFileTypeSupported(fileExtension) ? fileExtension : 'jsonc';
}

// Parse JSON content
const jsonContent = parseJSONContent(text, fileType as FileType);

// Check if the JSON content is null
if (jsonContent === null) {
return;
}

// Open the JSON document
const jsonDocument = await workspace.openTextDocument({
language: 'json',
content: JSON.stringify(jsonContent, null, 2),
});

// Show the JSON document
window.showTextDocument(jsonDocument);
}

/**
* The copyContent method.
*
Expand Down
1 change: 1 addition & 0 deletions src/app/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './feedback.controller';
export * from './files.controller';
export * from './json.controller';
export * from './transform.controller';
Loading

0 comments on commit 7b01a65

Please sign in to comment.