Skip to content

Commit 014ef66

Browse files
author
Brad Cornes
committed
prototyping emphasis/de-emphasis of class names
1 parent 6991b22 commit 014ef66

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

package-lock.json

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"dependencies": {
5656
"color": "^3.0.0",
5757
"dlv": "^1.1.1",
58+
"line-column": "^1.0.2",
5859
"tailwind-class-names": "0.6.0"
5960
},
6061
"author": "Brad Cornes <bradlc41@gmail.com>",

src/extension.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const htmlElements = require('./htmlElements.js')
66
const tailwindClassNames = require('tailwind-class-names')
77
const dlv = require('dlv')
88
const Color = require('color')
9+
const lineColumn = require('line-column')
910

1011
const CONFIG_GLOB =
1112
'**/{tailwind,tailwind.config,tailwind-config,.tailwindrc}.js'
@@ -409,6 +410,102 @@ class TailwindIntellisense {
409410
if (tailwind) {
410411
this._tailwind = tailwind
411412
this.reload(tailwind)
413+
414+
let fade = vscode.window.createTextEditorDecorationType({
415+
opacity: '0.5'
416+
})
417+
418+
this._providers.push(
419+
vscode.window.onDidChangeTextEditorSelection(e => {
420+
if (
421+
e.selections.length &&
422+
e.selections[0].start.line === e.selections[0].end.line &&
423+
e.selections[0].start.character === e.selections[0].end.character
424+
) {
425+
let position = e.selections[0].start
426+
let searchStart = new vscode.Position(
427+
Math.max(position.line - 5, 0),
428+
0
429+
)
430+
let searchEnd = new vscode.Position(position.line + 5, 0) // TODO: cap to end of file?
431+
let range: vscode.Range = new vscode.Range(searchStart, searchEnd)
432+
let text: string = e.textEditor.document.getText(range)
433+
434+
let regex = /(class=")([^"]+)"/g
435+
let matches
436+
let indexes = []
437+
let strings = []
438+
439+
while ((matches = regex.exec(text)) !== null) {
440+
strings.push(matches[2])
441+
indexes.push(
442+
regex.lastIndex - matches[0].length + matches[1].length
443+
)
444+
}
445+
446+
let addedDecoration = strings.some((str, i) => {
447+
let { line, col } = lineColumn(text).fromIndex(indexes[i])
448+
449+
let strStart = searchStart.translate({
450+
characterDelta: col - 1,
451+
lineDelta: line - 1
452+
})
453+
let { line: line1, col: col1 } = lineColumn(text).fromIndex(
454+
indexes[i] + str.length
455+
)
456+
let strEnd = searchStart.translate({
457+
characterDelta: col1 - 1,
458+
lineDelta: line1 - 1
459+
})
460+
if (
461+
position.isAfterOrEqual(strStart) &&
462+
position.isBeforeOrEqual(strEnd)
463+
) {
464+
let classNames = []
465+
regex = /\S+/g
466+
while ((matches = regex.exec(str)) !== null) {
467+
// strings.push(matches[2])
468+
// indexes.push(
469+
// regex.lastIndex - matches[0].length + matches[1].length
470+
// )
471+
let startIndex = regex.lastIndex - matches[0].length
472+
let endIndex = startIndex + matches[0].length
473+
let foo = lineColumn(str).fromIndex(startIndex)
474+
let bar = lineColumn(str).fromIndex(endIndex)
475+
classNames.push({
476+
className: matches[0],
477+
start: strStart.translate({
478+
lineDelta: foo.line - 1,
479+
characterDelta: foo.col - 1
480+
}),
481+
end: bar
482+
? strStart.translate({
483+
lineDelta: bar.line - 1,
484+
characterDelta: bar.col - 1
485+
})
486+
: strEnd
487+
})
488+
}
489+
let toFade = classNames
490+
.filter(x => {
491+
return !(
492+
position.isAfterOrEqual(x.start) &&
493+
position.isBeforeOrEqual(x.end)
494+
)
495+
})
496+
.map(x => ({ range: new vscode.Range(x.start, x.end) }))
497+
vscode.window.activeTextEditor.setDecorations(fade, toFade)
498+
return true
499+
}
500+
return false
501+
})
502+
503+
if (!addedDecoration) {
504+
vscode.window.activeTextEditor.setDecorations(fade, [])
505+
}
506+
}
507+
})
508+
)
412509
}
413510
}
414511

0 commit comments

Comments
 (0)