Skip to content

Commit a0c6229

Browse files
committed
tidy up
1 parent 743f10a commit a0c6229

9 files changed

+581
-563
lines changed

src/lsp/providers/codeActionProvider/index.ts renamed to src/lsp/providers/codeActions/codeActionProvider.ts

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { joinWithAnd } from '../../util/joinWithAnd'
3333
import { getLanguageBoundaries } from '../../util/getLanguageBoundaries'
3434
import { isCssDoc } from '../../util/css'
3535
import { absoluteRange } from '../../util/absoluteRange'
36+
import type { NodeSource, Root } from 'postcss'
3637

3738
async function getDiagnosticsFromCodeActionParams(
3839
state: State,
@@ -201,6 +202,19 @@ async function provideUtilityConflictsCodeActions(
201202
]
202203
}
203204

205+
function postcssSourceToRange(source: NodeSource): Range {
206+
return {
207+
start: {
208+
line: source.start.line - 1,
209+
character: source.start.column - 1,
210+
},
211+
end: {
212+
line: source.end.line - 1,
213+
character: source.end.column,
214+
},
215+
}
216+
}
217+
204218
async function provideInvalidApplyCodeActions(
205219
state: State,
206220
params: CodeActionParams,
@@ -211,7 +225,7 @@ async function provideInvalidApplyCodeActions(
211225
let cssRange: Range
212226
let cssText = documentText
213227
const { postcss } = state.modules
214-
let change: TextEdit
228+
let changes: TextEdit[] = []
215229

216230
let totalClassNamesInClassList = diagnostic.className.classList.classList.split(
217231
/\s+/
@@ -238,30 +252,18 @@ async function provideInvalidApplyCodeActions(
238252
try {
239253
await postcss([
240254
postcss.plugin('', (_options = {}) => {
241-
return (root) => {
255+
return (root: Root) => {
242256
root.walkRules((rule) => {
243-
if (change) return false
257+
if (changes.length) return false
244258

245259
rule.walkAtRules('apply', (atRule) => {
246-
let { start, end } = atRule.source
247-
let atRuleRange: Range = {
248-
start: {
249-
line: start.line - 1,
250-
character: start.column - 1,
251-
},
252-
end: {
253-
line: end.line - 1,
254-
character: end.column - 1,
255-
},
256-
}
260+
let atRuleRange = postcssSourceToRange(atRule.source)
257261
if (cssRange) {
258262
atRuleRange = absoluteRange(atRuleRange, cssRange)
259263
}
260264

261-
if (!isWithinRange(diagnostic.range.start, atRuleRange)) {
262-
// keep looking
265+
if (!isWithinRange(diagnostic.range.start, atRuleRange))
263266
return true
264-
}
265267

266268
let ast = classNameToAst(
267269
state,
@@ -270,35 +272,33 @@ async function provideInvalidApplyCodeActions(
270272
diagnostic.className.classList.important
271273
)
272274

273-
if (!ast) {
274-
return false
275-
}
275+
if (!ast) return false
276276

277277
rule.after(ast.nodes)
278278
let insertedRule = rule.next()
279+
if (!insertedRule) return false
279280

280281
if (totalClassNamesInClassList === 1) {
281282
atRule.remove()
283+
} else {
284+
changes.push({
285+
range: diagnostic.className.classList.range,
286+
newText: removeRangesFromString(
287+
diagnostic.className.classList.classList,
288+
diagnostic.className.relativeRange
289+
),
290+
})
282291
}
283292

284-
let outputIndent: string
285-
let documentIndent = detectIndent(documentText)
286-
287-
let ruleRange: Range = {
288-
start: {
289-
line: rule.source.start.line - 1,
290-
character: rule.source.start.column - 1,
291-
},
292-
end: {
293-
line: rule.source.end.line - 1,
294-
character: rule.source.end.column,
295-
},
296-
}
293+
let ruleRange = postcssSourceToRange(rule.source)
297294
if (cssRange) {
298295
ruleRange = absoluteRange(ruleRange, cssRange)
299296
}
300297

301-
change = {
298+
let outputIndent: string
299+
let documentIndent = detectIndent(documentText)
300+
301+
changes.push({
302302
range: ruleRange,
303303
newText:
304304
rule.toString() +
@@ -315,7 +315,7 @@ async function provideInvalidApplyCodeActions(
315315
documentIndent.indent
316316
)
317317
}),
318-
}
318+
})
319319

320320
return false
321321
})
@@ -327,7 +327,7 @@ async function provideInvalidApplyCodeActions(
327327
return []
328328
}
329329

330-
if (!change) {
330+
if (!changes.length) {
331331
return []
332332
}
333333

@@ -338,20 +338,7 @@ async function provideInvalidApplyCodeActions(
338338
diagnostics: [diagnostic],
339339
edit: {
340340
changes: {
341-
[params.textDocument.uri]: [
342-
...(totalClassNamesInClassList > 1
343-
? [
344-
{
345-
range: diagnostic.className.classList.range,
346-
newText: removeRangesFromString(
347-
diagnostic.className.classList.classList,
348-
diagnostic.className.relativeRange
349-
),
350-
},
351-
]
352-
: []),
353-
change,
354-
],
341+
[params.textDocument.uri]: changes,
355342
},
356343
},
357344
},

0 commit comments

Comments
 (0)