-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat add auto import for CSS Modules file (#961)
- Loading branch information
1 parent
b23bcd7
commit 7f14500
Showing
10 changed files
with
131 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { CompletionItem, CompletionItemKind, MarkdownString, SnippetString, TextEdit } from 'vscode'; | ||
|
||
export default function getCompletionItem( | ||
showText: string, | ||
itemKind: string, | ||
insertText?: string | SnippetString, | ||
documentation?: string | MarkdownString, | ||
additionalTextEdits?: TextEdit[], | ||
): CompletionItem { | ||
const completionItem = new CompletionItem(showText, CompletionItemKind[itemKind]); | ||
if (insertText) { | ||
completionItem.insertText = insertText; | ||
} | ||
if (documentation) { | ||
completionItem.documentation = documentation; | ||
} | ||
if (additionalTextEdits) { | ||
completionItem.additionalTextEdits = additionalTextEdits; | ||
} | ||
completionItem.detail = 'AppWorks'; | ||
completionItem.command = { command: 'style-helper.recordCompletionItemSelect', title: '' }; | ||
return completionItem; | ||
} |
18 changes: 0 additions & 18 deletions
18
extensions/style-helper/src/inlineStyleAutoComplete/getCompletionItem.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
extensions/style-helper/src/jsxVarStylesComplete/getFilenameWithoutExtname.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as path from 'path'; | ||
|
||
export default (fileName: string): string => { | ||
if (['.js', '.ts', '.jsx', '.tsx'].includes(path.extname(fileName))) { | ||
return path.basename(fileName, path.extname(fileName)); | ||
} else { | ||
return fileName; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as fs from 'fs'; | ||
import * as vscode from 'vscode'; | ||
import * as babelParser from '@babel/parser'; | ||
import getCompletionItem from '../getCompletionItem'; | ||
import { getFocusCodeInfo } from '../getFocusCodeInfo'; | ||
import getFilenameWithoutExtname from './getFilenameWithoutExtname'; | ||
|
||
function getImportDeclarations(tree) { | ||
const { body } = tree.program; | ||
return body.filter((node) => node.type === 'ImportDeclaration'); | ||
} | ||
|
||
function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position): vscode.CompletionItem[] { | ||
const editorText = document.getText(); | ||
const { Position, TextEdit } = vscode; | ||
const { directory, fileName } = getFocusCodeInfo(document, position); | ||
|
||
const completions: vscode.CompletionItem[] = []; | ||
|
||
// className={} | ||
completions.push(getCompletionItem('className={}', 'Text')); | ||
|
||
// if already use style or has import css file, ignore | ||
if ( | ||
editorText.indexOf('style') !== -1 || | ||
/\.(less|css|scss)$/.test(editorText) | ||
) { | ||
return completions; | ||
} | ||
|
||
// add styles completion item and auto import | ||
let newImport = ''; | ||
fs.readdirSync(directory).forEach((file) => { | ||
if (new RegExp(`${getFilenameWithoutExtname(fileName)}.module.(less|css|scss)$`).test(file)) { | ||
newImport = `import styles from './${file}';`; | ||
} | ||
}); | ||
|
||
if (newImport) { | ||
const ast = babelParser.parse(editorText, { | ||
// Support JSX and TS | ||
plugins: ['typescript', 'jsx'], | ||
sourceType: 'module', | ||
}); | ||
|
||
let positionForNewImport = new Position(0, 0); | ||
const importASTNodes = getImportDeclarations(ast); | ||
const lastImportNode = importASTNodes[importASTNodes.length - 1]; | ||
if (lastImportNode) { | ||
positionForNewImport = new Position(lastImportNode.loc.end.line, 0); | ||
} | ||
|
||
completions.push( | ||
getCompletionItem( | ||
'styles', 'Property', 'styles', | ||
new vscode.MarkdownString(`**Auto import** \n ${newImport}`), // Docs | ||
[TextEdit.insert(positionForNewImport, `${newImport}\n`)], | ||
), | ||
); | ||
} | ||
return completions; | ||
} | ||
|
||
export default function jsxVarStylesComplete(context: vscode.ExtensionContext): void { | ||
context.subscriptions.push( | ||
vscode.languages.registerCompletionItemProvider( | ||
[ | ||
{ scheme: 'file', language: 'javascriptreact' }, | ||
{ scheme: 'file', language: 'typescriptreact' }, | ||
], | ||
{ provideCompletionItems }, | ||
), | ||
); | ||
} |