Skip to content

Commit

Permalink
update code and change logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhongl524 committed Dec 23, 2024
1 parent 5b8eba7 commit facf5e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Improvements for the intellisense of tspconfig.yaml
- Support the auto completion for extends, imports, rule, rule sets and variables in tspconfig.yaml
- Show required/optional information in the details of emitter's options completion item in tspconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ packages:
- "@typespec/compiler"
---

Improvements for the intellisense of tspconfig.yaml
- Supports auto-completion of the extends and imports paths
- The rule or ruleSets of the linter can be auto-completed
- Emitter optoins autocomplete intelligently handles quotation mark display
- Autocomplete of variable interpolation
- The parameters of emitter's options distinguish whether they are required or optional
Fix bug in tspconfig.yaml
- Fix the issue that emitter option auto complete while inside "" will add extra ""`
35 changes: 20 additions & 15 deletions packages/compiler/src/server/tspconfig/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ export async function provideTspconfigCompletionItems(
}

// Variable interpolation
const variableInterpolationItems = resolveVariableInterpolationCompleteItems(
target.yamlDoc,
target.path,
tspConfigDoc.getText().slice(target.sourceRange?.pos, target.cursorPosition),
);
if (variableInterpolationItems.length > 0) {
return variableInterpolationItems;
if (target.sourceRange) {
const variableInterpolationItems = resolveVariableInterpolationCompleteItems(
target.yamlDoc,
target.path,
tspConfigDoc.getText().slice(target.sourceRange.pos, target.cursorPosition),
);
if (variableInterpolationItems.length > 0) {
return variableInterpolationItems;
}
}

const items = resolveTspConfigCompleteItems(
Expand Down Expand Up @@ -87,7 +89,7 @@ export async function provideTspconfigCompletionItems(
const items: CompletionItem[] = [];
for (const [name, pkg] of Object.entries(emitters)) {
if (!siblings.includes(name)) {
const item = createContainingQuatedValCompetionItem(
const item = createCompletionItemWithQuote(
name,
(await pkg.getPackageJsonData())?.description ?? `Emitter from ${name}`,
tspConfigPosition,
Expand Down Expand Up @@ -138,7 +140,7 @@ export async function provideTspconfigCompletionItems(
for (const [ruleSet] of Object.entries(exports?.$linter?.ruleSets)) {
const labelName = `${name}/${ruleSet}`;
if (!siblings.includes(labelName)) {
const item = createContainingQuatedValCompetionItem(
const item = createCompletionItemWithQuote(
labelName,
(await pkg.getPackageJsonData())?.description ?? `Linters from ${labelName}`,
tspConfigPosition,
Expand All @@ -151,9 +153,9 @@ export async function provideTspconfigCompletionItems(
}
}

// If there is no corresponding ruleSet in the library, add the library name directly.
// Add the library name directly.
if (!siblings.includes(name)) {
const item = createContainingQuatedValCompetionItem(
const item = createCompletionItemWithQuote(
name,
(await pkg.getPackageJsonData())?.description ?? `Linters from ${name}`,
tspConfigPosition,
Expand All @@ -174,7 +176,7 @@ export async function provideTspconfigCompletionItems(
exports?.$linter?.rules,
)) {
const labelName = `${name}/${rule.name}`;
const item = createContainingQuatedValCompetionItem(
const item = createCompletionItemWithQuote(
labelName,
rule.description,
tspConfigPosition,
Expand Down Expand Up @@ -359,7 +361,7 @@ export async function provideTspconfigCompletionItems(
* @param target The target object of the current configuration file, see {@link YamlScalarTarget}
* @returns CompletionItem object
*/
function createContainingQuatedValCompetionItem(
function createCompletionItemWithQuote(
labelName: string,
description: string,
tspConfigPosition: Position,
Expand All @@ -371,7 +373,7 @@ function createContainingQuatedValCompetionItem(
target.cursorPosition <= target.sourceRange.end
) {
// If it is a quoted string, the relative position needs to be reduced by 1
const relativePos =
const lenRelativeToStartPos =
target.sourceType === "QUOTE_SINGLE" || target.sourceType === "QUOTE_DOUBLE"
? target.cursorPosition - target.sourceRange.pos - 1
: target.cursorPosition - target.sourceRange.pos;
Expand All @@ -381,7 +383,10 @@ function createContainingQuatedValCompetionItem(
documentation: description,
textEdit: TextEdit.replace(
Range.create(
Position.create(tspConfigPosition.line, tspConfigPosition.character - relativePos),
Position.create(
tspConfigPosition.line,
tspConfigPosition.character - lenRelativeToStartPos,
),
Position.create(tspConfigPosition.line, tspConfigPosition.character),
),
target.sourceType === "QUOTE_SINGLE" || target.sourceType === "QUOTE_DOUBLE"
Expand Down

0 comments on commit facf5e5

Please sign in to comment.