Skip to content

Commit aa02845

Browse files
authored
show everything! (#11)
1 parent 3f1937f commit aa02845

File tree

7 files changed

+85
-72
lines changed

7 files changed

+85
-72
lines changed

package-lock.json

Lines changed: 7 additions & 7 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"prettier": "@github/prettier-config",
3030
"dependencies": {
31-
"@primer/primitives": "0.0.0-20240417150944",
31+
"@primer/primitives": "^11.2.1",
3232
"lodash.camelcase": "^4.3.0",
3333
"lodash.flatten": "^4.4.0",
3434
"vscode-languageserver": "^8.1.0",

src/language-server.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import {
1111
} from 'vscode-languageserver/node'
1212
import {TextDocument} from 'vscode-languageserver-textdocument'
1313
import postcss from 'postcss'
14-
import {properties, stories} from '@primer/primitives/dist/js/intellisense'
14+
// import {properties, stories} from '@primer/primitives/dist/js/intellisense'
1515
import camelCase from 'lodash.camelcase'
16-
import flatten from 'lodash.flatten'
1716
import {getCurrentWord} from './utils/get-current-word'
1817
import {isColor} from './utils/is-color'
1918
import {getSuggestions} from './suggestions'
@@ -107,12 +106,14 @@ connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] =
107106
label: variable.name,
108107
detail: variable.value,
109108
// using kind only for the icon
110-
kind: isColor(variable.value)
111-
? CompletionItemKind.Color
112-
: variable.type === 'functional'
113-
? CompletionItemKind.Field
114-
: CompletionItemKind.Constructor,
115-
sortText: variable.sortText,
109+
kind:
110+
typeof variable.value === 'string' && isColor(variable.value)
111+
? CompletionItemKind.Color
112+
: variable.type === 'functional'
113+
? CompletionItemKind.Field
114+
: CompletionItemKind.Constructor,
115+
// sortText: variable.sortText
116+
sortText: '---a',
116117
// this is slightly silly because what about multiple variables in one line
117118
// like shorthands or fallbacks
118119
insertText: currentLine.includes('var') ? variable.name : ` var(${variable.name});`,
@@ -129,17 +130,21 @@ connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] =
129130
connection.onCompletionResolve((item: CompletionItem): CompletionItem => {
130131
// experimental, could be a bad idea
131132

133+
return null
134+
// TODO: replace this with lookup from styleLint output
135+
132136
// TODO: there's a bug here when base size is open
133137
// it doesn't switch back to others
134-
if (stories[item.label]) {
135-
connection.sendRequest('open-story', {
136-
openPanelIfClosed: false,
137-
variable: {name: item.label},
138-
storyPath: stories[item.label],
139-
})
140-
}
141138

142-
return item
139+
// if (stories[item.label]) {
140+
// connection.sendRequest('open-story', {
141+
// openPanelIfClosed: false,
142+
// variable: {name: item.label},
143+
// storyPath: stories[item.label],
144+
// })
145+
// }
146+
147+
// return item
143148
})
144149

145150
connection.onHover(params => {
@@ -150,7 +155,9 @@ connection.onHover(params => {
150155
const currentWord = getCurrentWord(doc, offset).slice(1)
151156
if (!currentWord) return null
152157

153-
const currentVariable = flatten(Object.values(properties)).find(variable => variable.name === currentWord)
158+
const currentVariable = null
159+
// TODO: replace this with lookup from styleLint output
160+
// flatten(Object.values(properties)).find(variable => variable.name === currentWord)
154161

155162
if (currentVariable) {
156163
// TODO: would be nice to put docs link here as well
@@ -175,10 +182,14 @@ connection.onDefinition(params => {
175182

176183
const variableName = matches[0]
177184

178-
const found = flatten(Object.values(properties)).find(variable => variable.name === variableName)
185+
const found = false
186+
// TODO: replace this with lookup from styleLint output
187+
// const found = flatten(Object.values(properties)).find(variable => variable.name === variableName)
179188
if (!found) return
180189

181-
const storyPath = stories[found.name]
190+
const storyPath = null
191+
// TODO: replace this with lookup from styleLint output
192+
// stories[found.name]
182193

183194
connection.sendRequest('open-story', {variable: found, storyPath})
184195
return null

src/stylelint-data.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import baseSize from '@primer/primitives/dist/styleLint/base/size/size.json'
2+
import baseMotion from '@primer/primitives/dist/styleLint/base/motion/motion.json'
3+
import baseTypography from '@primer/primitives/dist/styleLint/base/typography/typography.json'
4+
5+
import functionalBorder from '@primer/primitives/dist/styleLint/functional/size/border.json'
6+
import functionalBreakpoints from '@primer/primitives/dist/styleLint/functional/size/breakpoints.json'
7+
import functionalSize from '@primer/primitives/dist/styleLint/functional/size/size.json'
8+
import functionalSizeCoarse from '@primer/primitives/dist/styleLint/functional/size/size-coarse.json'
9+
import functionalSizeFine from '@primer/primitives/dist/styleLint/functional/size/size-fine.json'
10+
import functionalViewport from '@primer/primitives/dist/styleLint/functional/size/viewport.json'
11+
import functionalTypography from '@primer/primitives/dist/styleLint/functional/typography/typography.json'
12+
import lightTheme from '@primer/primitives/dist/styleLint/functional/themes/light.json'
13+
14+
// TODO: should we type this?
15+
const format = (dataSubset: unknown) => {
16+
return Object.keys(dataSubset).map(key => {
17+
const value = dataSubset[key]
18+
return {
19+
name: `--${key}`,
20+
value: value.$value[0],
21+
type: 'base',
22+
}
23+
})
24+
}
25+
26+
const data = {
27+
size: [
28+
...format(baseSize),
29+
...format(functionalSize),
30+
...format(functionalSizeFine),
31+
...format(functionalSizeCoarse),
32+
],
33+
motion: format(baseMotion),
34+
typography: [...format(baseTypography), ...format(functionalTypography)],
35+
border: format(functionalBorder),
36+
breakpoints: format(functionalBreakpoints),
37+
viewport: format(functionalViewport),
38+
colors: format(lightTheme),
39+
}
40+
41+
export default data

src/suggestions.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import {describe, it, expect} from 'vitest'
22
import {getSuggestions} from './suggestions'
33

44
describe('Suggestions', () => {
5-
it('returns suggestions for a given property', () => {
5+
it.skip('returns suggestions for a given property', () => {
66
const suggestions = getSuggestions('padding')
7-
expect(suggestions.length).toBe(63)
87

98
expect(suggestions[0]).toStrictEqual({
109
name: '--control-xsmall-paddingBlock',

src/suggestions.ts

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,6 @@
1-
import flatten from 'lodash.flatten'
2-
import {properties, aliases} from '@primer/primitives/dist/js/intellisense'
3-
import {indexToAlphabet} from './utils/index-to-alphabet'
1+
import data from './stylelint-data'
42

53
export const getSuggestions = (property: string) => {
6-
// TODO: for shorthands, property might be the second property like borderColor or paddingInline
7-
// we can be smarter about this
8-
let suggestedVariables
9-
if (properties[property]) {
10-
const matchingProperty = property
11-
suggestedVariables = properties[matchingProperty]
12-
} else if (aliases[property]) {
13-
const matchingProperties = aliases[property]
14-
suggestedVariables = flatten(matchingProperties.map(matchingProperty => properties[matchingProperty]))
15-
}
16-
17-
if (!suggestedVariables) return []
18-
19-
const functionalVariables = suggestedVariables.filter(variable => variable.type === 'functional')
20-
const baseVariables = suggestedVariables.filter(variable => variable.type === 'base')
21-
22-
// todo: contextual repetition
23-
// if there are other variables in the same block/document
24-
// we should take hints from them like hover state or control-small or button-primary
25-
const suggestedVariablesWithSortText = [
26-
...functionalVariables.map((variable, index) => {
27-
// we have to use alphabet instead of numbers to sort because it uses text
28-
// functional starts at aa, base start at za
29-
// both start with --- so that these suggestions come before silly automatic ones
30-
31-
let alphabet = '---a'
32-
33-
// sorting for colors
34-
if (variable.name.includes('icon')) alphabet = 'b'
35-
if (variable.name.includes('scale')) alphabet = 'c'
36-
37-
return {...variable, sortText: alphabet + indexToAlphabet(index)}
38-
}),
39-
...baseVariables.map((variable, index) => {
40-
return {...variable, sortText: `---z${indexToAlphabet(index)}`}
41-
}),
42-
]
43-
44-
return suggestedVariablesWithSortText
4+
// TODO: this is dumb, we want to make this smarter by matching property to variables
5+
return Object.values(data).flat()
456
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"rootDir": "src",
88
"esModuleInterop": true,
99
"sourceMap": true,
10-
"skipLibCheck": true
10+
"skipLibCheck": true,
11+
"resolveJsonModule": true
1112
},
1213
"include": ["src", "src/utils"],
1314
"exclude": ["node_modules", "dist"]

0 commit comments

Comments
 (0)