Skip to content

Commit

Permalink
Display result next to each incremental definition (#233)
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Eknert <anders@eknert.com>
  • Loading branch information
anderseknert authored Aug 12, 2024
1 parent 01f48b3 commit 8948c77
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions src/ls/clients/regal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,6 @@ function handleRegalShowEvalResult(params: any) {
return
}

const line = params.line - 1;
const documentLine = activeEditor.document.lineAt(line);
const lineLength = documentLine.text.length;

// attachmentMessage is the message that is displayed after the rule name within the editor
let attachmentMessage = params.result.value

Expand Down Expand Up @@ -305,6 +301,10 @@ function handleRegalShowEvalResult(params: any) {
}
}

const line = params.line - 1;
const documentLine = activeEditor.document.lineAt(line);
const lineLength = documentLine.text.length;

// to avoid horizontal scroll for large outputs, we ask users to hover
// for the full result
const truncateThreshold = 100;
Expand All @@ -313,21 +313,6 @@ function handleRegalShowEvalResult(params: any) {
attachmentMessage = attachmentMessage.substring(0, truncateThreshold - lineLength - suffix.length) + suffix;
}

const decorationOption: vscode.DecorationOptions = {
// this is not needed as these options are passed to a whole line decoration type
// however, the field is required.
range: new vscode.Range(new vscode.Position(line, 0), new vscode.Position(line, lineLength),),
hoverMessage: hoverMessage,
renderOptions: {
after: {
contentText: " => " + attachmentMessage,
// Using the same color as the line numbers means this matches
// the 'muted' appearance of the gutter for various themes
color: new vscode.ThemeColor('editorLineNumber.foreground'),
},
},
}

let ruleEnd = lineLength;
// find the first whitespace, or [ char, if found, update endRange to that
for (let i = 0; i < lineLength; i++) {
Expand All @@ -337,30 +322,55 @@ function handleRegalShowEvalResult(params: any) {
}
}

// ruleNameDecorationOptions highlights only the rule name with a color.
// the colored highlight is displayed in addition to the whole line decoration
const ruleNameDecorationOptions: vscode.DecorationOptions = {
range: new vscode.Range(
new vscode.Position(line, 0),
new vscode.Position(line, ruleEnd),
),
};
const ruleName = documentLine.text.substring(0, ruleEnd)

const decorationOptions: vscode.DecorationOptions[] = []
const ruleNameDecorationOptions: vscode.DecorationOptions[] = []

for (let line = 0; line < activeEditor.document.lineCount; line++) {
if (!activeEditor.document.lineAt(line).text.startsWith(ruleName)) {
continue
}

decorationOptions.push({
// this is not needed as these options are passed to a whole line decoration type
// however, the field is required.
range: new vscode.Range(new vscode.Position(line, 0), new vscode.Position(line, lineLength)),
hoverMessage: hoverMessage,
renderOptions: {
after: {
contentText: " => " + attachmentMessage,
// Using the same color as the line numbers means this matches
// the 'muted' appearance of the gutter for various themes
color: new vscode.ThemeColor('editorLineNumber.foreground'),
},
},
});

// ruleNameDecorationOptions highlights only the rule name with a color.
// the colored highlight is displayed in addition to the whole line decoration
ruleNameDecorationOptions.push({
range: new vscode.Range(
new vscode.Position(line, 0),
new vscode.Position(line, ruleEnd),
),
});
}

// before setting a new decoration, remove all previous decorations
removeDecorations();

// always set the base decoration, containing the result message and after text
activeEditor.setDecorations(evalResultDecorationType, [decorationOption]);
activeEditor.setDecorations(evalResultDecorationType, decorationOptions);

// evalResultDecorationTypeUndefined is a different color to indicate
// the difference between undefined and other results
if (params.result.isUndefined) {
activeEditor.setDecorations(evalResultTargetUndefinedDecorationType, [ruleNameDecorationOptions]);
return
activeEditor.setDecorations(evalResultTargetUndefinedDecorationType, ruleNameDecorationOptions);
} else {
// otherwise, show a success decoration for the rule
activeEditor.setDecorations(evalResultTargetSuccessDecorationType, ruleNameDecorationOptions);
}

// otherwise, show a success decoration for the rule
activeEditor.setDecorations(evalResultTargetSuccessDecorationType, [ruleNameDecorationOptions]);
}

// makeCode returns a markdown code block with the given language and code
Expand Down

0 comments on commit 8948c77

Please sign in to comment.