Skip to content

Commit 34ee91d

Browse files
committed
Improve file path completions
1 parent 1f38e22 commit 34ee91d

File tree

2 files changed

+96
-11
lines changed

2 files changed

+96
-11
lines changed

packages/tailwindcss-language-service/src/completionProvider.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
addPixelEquivalentsToValue,
3838
} from './util/pixelEquivalents'
3939
import { customClassesIn } from './util/classes'
40+
import { IS_SCRIPT_SOURCE, IS_TEMPLATE_SOURCE } from './metadata/extensions'
4041
import * as postcss from 'postcss'
4142

4243
let isUtil = (className) =>
@@ -1537,30 +1538,38 @@ async function provideFileDirectiveCompletions(
15371538
}
15381539

15391540
let pattern = state.v4
1540-
? /@(config|plugin|source)\s*(?<partial>'[^']*|"[^"]*)$/
1541-
: /@config\s*(?<partial>'[^']*|"[^"]*)$/
1541+
? /@(?<directive>config|plugin|source)\s*(?<partial>'[^']*|"[^"]*)$/
1542+
: /@(?<directive>config)\s*(?<partial>'[^']*|"[^"]*)$/
15421543

15431544
let text = document.getText({ start: { line: position.line, character: 0 }, end: position })
15441545
let match = text.match(pattern)
15451546
if (!match) {
15461547
return null
15471548
}
1549+
let directive = match.groups.directive
15481550
let partial = match.groups.partial.slice(1) // remove quote
15491551
let valueBeforeLastSlash = partial.substring(0, partial.lastIndexOf('/'))
15501552
let valueAfterLastSlash = partial.substring(partial.lastIndexOf('/') + 1)
15511553

1554+
let entries = await state.editor.readDirectory(document, valueBeforeLastSlash || '.')
1555+
1556+
let isAllowedFile = directive === 'source' ? IS_TEMPLATE_SOURCE : IS_SCRIPT_SOURCE
1557+
1558+
// Only show directories and JavaScript/TypeScript files
1559+
entries = entries.filter(([name, type]) => {
1560+
return type.isDirectory || isAllowedFile.test(name)
1561+
})
1562+
15521563
return withDefaults(
15531564
{
15541565
isIncomplete: false,
1555-
items: (await state.editor.readDirectory(document, valueBeforeLastSlash || '.'))
1556-
.filter(([name, type]) => type.isDirectory || /\.c?js$/.test(name))
1557-
.map(([name, type]) => ({
1558-
label: type.isDirectory ? name + '/' : name,
1559-
kind: type.isDirectory ? 19 : 17,
1560-
command: type.isDirectory
1561-
? { command: 'editor.action.triggerSuggest', title: '' }
1562-
: undefined,
1563-
})),
1566+
items: entries.map(([name, type]) => ({
1567+
label: type.isDirectory ? name + '/' : name,
1568+
kind: type.isDirectory ? 19 : 17,
1569+
command: type.isDirectory
1570+
? { command: 'editor.action.triggerSuggest', title: '' }
1571+
: undefined,
1572+
})),
15641573
},
15651574
{
15661575
data: {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
let scriptExtensions = [
2+
// JS
3+
'js',
4+
'cjs',
5+
'mjs',
6+
'ts',
7+
'mts',
8+
'cts',
9+
]
10+
11+
let templateExtensions = [
12+
// HTML
13+
'html',
14+
'pug',
15+
16+
// Glimmer
17+
'gjs',
18+
'gts',
19+
20+
// JS
21+
'astro',
22+
'cjs',
23+
'cts',
24+
'jade',
25+
'js',
26+
'jsx',
27+
'mjs',
28+
'mts',
29+
'svelte',
30+
'ts',
31+
'tsx',
32+
'vue',
33+
34+
// Markdown
35+
'md',
36+
'mdx',
37+
38+
// ASP
39+
'aspx',
40+
'razor',
41+
42+
// Handlebars
43+
'handlebars',
44+
'hbs',
45+
'mustache',
46+
47+
// PHP
48+
'php',
49+
'twig',
50+
51+
// Ruby
52+
'erb',
53+
'haml',
54+
'liquid',
55+
'rb',
56+
'rhtml',
57+
'slim',
58+
59+
// Elixir / Phoenix
60+
'eex',
61+
'heex',
62+
63+
// Nunjucks
64+
'njk',
65+
'nunjucks',
66+
67+
// Python
68+
'py',
69+
'tpl',
70+
71+
// Rust
72+
'rs',
73+
]
74+
75+
export const IS_SCRIPT_SOURCE = new RegExp(`\\.(${scriptExtensions.join('|')})$`)
76+
export const IS_TEMPLATE_SOURCE = new RegExp(`\\.(${templateExtensions.join('|')})$`)

0 commit comments

Comments
 (0)