Skip to content

Commit

Permalink
Merge pull request #3 from vscodeshift/fix/tabs
Browse files Browse the repository at this point in the history
fix: use editor tab settings
  • Loading branch information
jedwards1211 authored Mar 11, 2020
2 parents 610cb90 + 2e0cf4e commit 3a5ba84
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 30 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@
},
"dependencies": {
"@babel/runtime": "^7.1.5",
"@vscodeshift/apply-jscodeshift": "^1.0.12",
"jscodeshift": "^0.7.0",
"jss-codemorphs": "^1.0.0"
},
Expand Down
61 changes: 58 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,64 @@
import * as vscode from 'vscode'
import applyTransform from '@vscodeshift/apply-jscodeshift'
import { convertCssToJssString } from 'jss-codemorphs/convertCssToJss'

export function activate(context: vscode.ExtensionContext): void {
const disposable = vscode.commands.registerCommand('extension.cssToJss', () =>
applyTransform(require('jss-codemorphs/css-to-jss'))
const disposable = vscode.commands.registerCommand(
'extension.cssToJss',
async (): Promise<void> => {
const { window } = vscode
const { activeTextEditor: editor } = window
if (!editor) return

const selectionStart = editor.document.offsetAt(editor.selection.start)
const selectionEnd = editor.document.offsetAt(editor.selection.end)

let code = editor.document.getText()
if (selectionStart != selectionEnd) {
code = code.substring(selectionStart, selectionEnd)
}

const { tabSize, insertSpaces } = editor.options
// recast printing with custom tab format isn't reliable,
// so just use spaces and then replace them with the editor setting
let converted = convertCssToJssString(code, {
tabWidth: 2,
useTabs: false,
})
const tabChars = insertSpaces
? ' '.repeat(typeof tabSize === 'number' ? tabSize : 2)
: '\t'
converted = converted.replace(/^ */gm, match =>
tabChars.repeat(match.length / 2)
)

const indentation = /^(\s*)\S/gm

const firstLineBreak = /[\r\n]/.exec(code)

let indent: string | null = null
let match
while ((match = indentation.exec(code))) {
if (firstLineBreak && match.index < firstLineBreak.index) continue
if (indent == null || match[1].length < indent.length) {
indent = match[1]
}
}
if (indent) {
if (typeof insertSpaces === 'boolean' && typeof tabSize === 'number') {
const tabChars = insertSpaces ? ' '.repeat(tabSize) : '\t'
indent = indent.replace(new RegExp(`\t| {${tabSize}}`, 'g'), tabChars)
}
converted = converted.replace(/^/gm, indent)
}
const initialWhitespace = /^\s*/.exec(code)
if (initialWhitespace) {
converted = converted.replace(/^\s*/, initialWhitespace[0])
}

await editor.edit(edit => {
edit.replace(editor.selection, converted)
})
}
)

context.subscriptions.push(disposable)
Expand Down
43 changes: 42 additions & 1 deletion src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode'
import { expect } from 'chai'

suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.')
test('extension.cssToJss command', async function() {
const document = await vscode.workspace.openTextDocument(
vscode.Uri.parse('untitled:test.js')
)
const editor = await vscode.window.showTextDocument(document)
editor.options.tabSize = 2
editor.options.insertSpaces = true
await editor.edit(edit =>
edit.insert(
document.positionAt(0),
`{
body {
margin: 0;
overflow: hidden;
display: flex;
height: 100vh;
background: black;
}
}
`
)
)
editor.selection = new vscode.Selection(
new vscode.Position(1, 2),
new vscode.Position(7, 3)
)
await vscode.commands.executeCommand('extension.cssToJss')

expect(document.getText()).to.equal(`{
'@global': {
body: {
margin: '0',
overflow: 'hidden',
display: 'flex',
height: '100vh',
background: 'black'
}
},
}
`)
})
})
28 changes: 3 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1332,14 +1332,6 @@
semver "^6.3.0"
tsutils "^3.17.1"

"@vscodeshift/apply-jscodeshift@^1.0.12":
version "1.0.12"
resolved "https://registry.yarnpkg.com/@vscodeshift/apply-jscodeshift/-/apply-jscodeshift-1.0.12.tgz#d3a9bd00f8e6d69403b7a2763a9765a399b26150"
integrity sha512-da/1k4koJLUWJvAVEor8tJV3MoB2zMXC00G2padpdMRejUpQzZ645htGwYhhR0tfd4Oult+dAmi8T08QhwxR2Q==
dependencies:
"@babel/runtime" "^7.1.5"
jscodeshift-choose-parser "^1.0.0"

JSONStream@^1.0.4, JSONStream@^1.3.4, JSONStream@^1.3.5:
version "1.3.5"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
Expand Down Expand Up @@ -5104,13 +5096,6 @@ jsbn@~0.1.0:
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
integrity sha1-peZUwuWi3rXyAdls77yoDA7y9RM=

jscodeshift-choose-parser@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/jscodeshift-choose-parser/-/jscodeshift-choose-parser-1.0.0.tgz#b3a67ca67f3fa27cce7fff1b206175627a79daf6"
integrity sha512-577IQSYouUkx4+7cDa+QAMo6jbUdDZMfGBYy4/Xv8NKMapEhF8Xz9FvNftBsPV4uqyP4eh7QiXL8LZduNW54AQ==
dependencies:
resolve "^1.15.0"

jscodeshift-paths-in-range@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/jscodeshift-paths-in-range/-/jscodeshift-paths-in-range-1.1.0.tgz#accc511afd807094f274a497f136b6d338efda8d"
Expand Down Expand Up @@ -5212,9 +5197,9 @@ jsprim@^1.2.2:
verror "1.10.0"

jss-codemorphs@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/jss-codemorphs/-/jss-codemorphs-1.0.1.tgz#625bd7603ecc0e6088fb5f40967c627752000942"
integrity sha512-hjg28hG8dVSahb+5abGIRbg5CY7H1BzlEnTueL0iztLsRqF1ilsRT+SYRK9YUuDCLfxNOLKN5U8quk31ijTi6g==
version "1.2.0"
resolved "https://registry.yarnpkg.com/jss-codemorphs/-/jss-codemorphs-1.2.0.tgz#486ffaf4393e457159738df4e787a7fafa604c5d"
integrity sha512-pPpAeTcB070CiwBvAdCoEcG94xLGV7ZPDTseLdU0nrZaE/A+hU2+D0igzCYSsuyEfzWGMgPILCyiO0fTIU7YPw==
dependencies:
"@babel/runtime" "^7.1.5"
jscodeshift-paths-in-range "^1.0.0"
Expand Down Expand Up @@ -7881,13 +7866,6 @@ resolve@^1.10.0, resolve@^1.12.0, resolve@^1.3.2, resolve@^1.8.1:
dependencies:
path-parse "^1.0.6"

resolve@^1.15.0:
version "1.15.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.15.0.tgz#1b7ca96073ebb52e741ffd799f6b39ea462c67f5"
integrity sha512-+hTmAldEGE80U2wJJDC1lebb5jWqvTYAfm3YZ1ckk1gBr0MnCqUKlwK1e+anaFljIl+F5tR5IoZcm4ZDA1zMQw==
dependencies:
path-parse "^1.0.6"

restore-cursor@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"
Expand Down

0 comments on commit 3a5ba84

Please sign in to comment.