/
code-actions.ts
155 lines (133 loc) · 5.53 KB
/
code-actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { SnippetString } from 'vscode';
import { showDirectoryPicker } from "./directories-picker";
import { showFilePicker } from "./file-picker";
import { activeEditor, selectedText, activeFileName, openFile, selectedTextStart, selectedTextEnd, showErrorMessage, showInformationMessage, allText } from "./editor";
import { statelessToStateful } from "./modules/statless-to-stateful";
import { statefulToStateless } from './modules/stateful-to-stateless'
import { shouldSwitchToTarget, shouldBeConsideredJsFiles } from "./settings";
import { replaceTextInFile, appendTextToFile, prependTextToFile, removeContentFromFileAtLineAndColumn } from "./file-system";
import { getIdentifier, generateImportStatementFromFile, transformJSIntoExportExpressions } from "./parsing";
import { createComponentInstance, wrapWithComponent, isRangeContainedInJSXExpression, isJSXExpression } from "./modules/jsx";
import * as relative from 'relative';
import * as path from 'path';
export async function extractJSXToComponent() {
var editor = activeEditor();
if (!editor) {
return; // No open text editor
}
try {
const folderPath = await showDirectoryPicker()
const filePath = await showFilePicker(folderPath);
const selectionProccessingResult = await wrapWithComponent(filePath, selectedText());
await appendSelectedTextToFile(selectionProccessingResult, filePath);
await prependImportsToFileIfNeeded(selectionProccessingResult, filePath);
const componentInstance = createComponentInstance(selectionProccessingResult.metadata.name, selectionProccessingResult.metadata.componentProperties);
await replaceSelectionWith(componentInstance);
await switchToDestinationFileIfRequired(filePath);
} catch (e) {
handleError(e);
}
}
export async function wrapJSXWithCondition() {
var editor = activeEditor();
if (!editor) {
return; // No open text editor
}
try {
const selText = selectedText();
const isParentJSXExpression = isRangeContainedInJSXExpression(allText(), selectedTextStart(), selectedTextEnd());
const conditionalJSX = isJSXExpression(selText) ? selText : `<>${selText}</>`;
const snippetInnerText = `\n$\{1:true\}\n? ${conditionalJSX}\n: $\{2:null\}\n`;
const snippetText = isParentJSXExpression ? `{${snippetInnerText}}` : `(${snippetInnerText})`;
await editor.insertSnippet(new SnippetString(snippetText));
} catch (e) {
handleError(e);
}
}
export async function extractToFile() {
var editor = activeEditor();
if (!editor) {
return; // No open text editor
}
try {
const folderPath = await showDirectoryPicker()
const filePath = await showFilePicker(folderPath);
const selectionProccessingResult: ProcessedSelection = {
text: selectedText(),
metadata: {}
};
await appendSelectedTextToFile(selectionProccessingResult, filePath);
await removeSelectedTextFromOriginalFile(selectionProccessingResult);
await prependImportsToFileIfNeeded(selectionProccessingResult, filePath);
await switchToDestinationFileIfRequired(filePath);
} catch (e) {
handleError(e);
}
}
export async function statelessToStatefulComponent() {
try {
const selectionProccessingResult = statelessToStateful(selectedText())
await replaceSelectionWith(selectionProccessingResult.text);
} catch (e) {
handleError(e);
}
}
export async function statefulToStatelessComponent() {
try {
await showInformationMessage('WARNING! All lifecycle methods and react instance methods would be removed. Are you sure you want to continue?', ['Yes', 'No']).then(async res => {
if (res === 'Yes') {
const selectionProccessingResult = statefulToStateless(selectedText())
await replaceSelectionWith(selectionProccessingResult.text);
}
});
} catch (e) {
handleError(e);
}
}
export async function switchToDestinationFileIfRequired(destinationFilePath: any) {
if (shouldSwitchToTarget()) {
await openFile(destinationFilePath);
}
}
export async function replaceSelectionWith(text: string, path = activeFileName()) {
await replaceTextInFile(text, selectedTextStart(), selectedTextEnd(), activeFileName());
}
export type ProcessedSelection = {
text: string;
metadata: any;
};
export const appendSelectedTextToFile = ({
text: selection
}, destinationPath) => {
let text;
if (isOperationBetweenJSFiles(destinationPath)) {
text = transformJSIntoExportExpressions(selection);
} else {
text = selection;
}
return appendTextToFile(`
${text}
`, destinationPath);
};
export const prependImportsToFileIfNeeded = ({
text: selection
}, destinationFilePath) => {
if (!isOperationBetweenJSFiles(destinationFilePath)) return;
const originFilePath = activeFileName();
const identifiers = getIdentifier(selection);
const destinationPathRelativeToOrigin = relative(originFilePath, destinationFilePath);
const destinationFileName = path.parse(destinationPathRelativeToOrigin).name;
const destinationModule = [...destinationPathRelativeToOrigin.split('/').slice(0, -1), destinationFileName].join('/');
const importStatement = generateImportStatementFromFile(identifiers, destinationModule);
return prependTextToFile(importStatement, originFilePath);
};
export const removeSelectedTextFromOriginalFile = selection => {
let content = '';
return removeContentFromFileAtLineAndColumn(selectedTextStart(), selectedTextEnd(), activeFileName(), content);
};
export const isOperationBetweenJSFiles = destinationPath => shouldBeConsideredJsFiles(activeFileName(), destinationPath);
export const handleError = e => {
if (e) {
showErrorMessage(e.message);
}
};