|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as l10n from '@vscode/l10n'; |
| 7 | +import type { ChatPromptReference } from 'vscode'; |
| 8 | +import { isLocation } from '../../../util/common/types'; |
| 9 | +import { coalesce } from '../../../util/vs/base/common/arrays'; |
| 10 | +import { Codicon } from '../../../util/vs/base/common/codicons'; |
| 11 | +import { basename } from '../../../util/vs/base/common/resources'; |
| 12 | +import { isNumber, isString } from '../../../util/vs/base/common/types'; |
| 13 | +import { URI } from '../../../util/vs/base/common/uri'; |
| 14 | +import { Range as InternalRange } from '../../../util/vs/editor/common/core/range'; |
| 15 | +import { SymbolKind } from '../../../util/vs/workbench/api/common/extHostTypes/symbolInformation'; |
| 16 | +import { ChatReferenceDiagnostic, Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, Range, Uri } from '../../../vscodeTypes'; |
| 17 | +import { Attachment } from '@github/copilot/sdk'; |
| 18 | +import { ResourceSet } from '../../../util/vs/base/common/map'; |
| 19 | + |
| 20 | +export function convertReferenceToVariable(ref: ChatPromptReference, attachments: readonly Attachment[]) { |
| 21 | + const value = ref.value; |
| 22 | + const range = ref.range ? { start: ref.range[0], endExclusive: ref.range[1] } : undefined; |
| 23 | + |
| 24 | + if (value && value instanceof ChatReferenceDiagnostic && Array.isArray(value.diagnostics) && value.diagnostics.length && value.diagnostics[0][1].length) { |
| 25 | + const marker = DiagnosticConverter.from(value.diagnostics[0][1][0]); |
| 26 | + const refValue = { |
| 27 | + filterRange: { startLineNumber: marker.startLineNumber, startColumn: marker.startColumn, endLineNumber: marker.endLineNumber, endColumn: marker.endColumn }, |
| 28 | + filterSeverity: marker.severity, |
| 29 | + filterUri: value.diagnostics[0][0], |
| 30 | + problemMessage: value.diagnostics[0][1][0].message |
| 31 | + }; |
| 32 | + return IDiagnosticVariableEntryFilterData.toEntry(refValue); |
| 33 | + } |
| 34 | + |
| 35 | + if (isLocation(ref.value) && ref.name.startsWith(`sym:`)) { |
| 36 | + return { |
| 37 | + id: ref.id, |
| 38 | + name: ref.name, |
| 39 | + fullName: ref.name.substring(4), |
| 40 | + value: { uri: ref.value.uri, range: toInternalRange(ref.value.range) }, |
| 41 | + // We never send this information to extensions, so default to Property |
| 42 | + symbolKind: SymbolKind.Property, |
| 43 | + // We never send this information to extensions, so default to Property |
| 44 | + icon: Codicon.symbolProperty, |
| 45 | + kind: 'symbol', |
| 46 | + range, |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + if (URI.isUri(value) && ref.name.startsWith(`prompt:`) && |
| 51 | + ref.id.startsWith(PromptFileVariableKind.PromptFile) && |
| 52 | + ref.id.endsWith(value.toString())) { |
| 53 | + return { |
| 54 | + id: ref.id, |
| 55 | + name: `prompt:${basename(value)}`, |
| 56 | + value, |
| 57 | + kind: 'promptFile', |
| 58 | + modelDescription: 'Prompt instructions file', |
| 59 | + isRoot: true, |
| 60 | + automaticallyAdded: false, |
| 61 | + range, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + const folders = new ResourceSet(attachments.filter(att => att.type === 'directory').map(att => URI.file(att.path))); |
| 66 | + const isFile = URI.isUri(value) || (value && typeof value === 'object' && 'uri' in value); |
| 67 | + const isFolder = isFile && URI.isUri(value) && (value.path.endsWith('/') || folders.has(value)); |
| 68 | + return { |
| 69 | + id: ref.id, |
| 70 | + name: ref.name, |
| 71 | + value, |
| 72 | + modelDescription: ref.modelDescription, |
| 73 | + range, |
| 74 | + kind: isFolder ? 'directory' as const : isFile ? 'file' as const : 'generic' as const |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +function toInternalRange(range: Range): InternalRange { |
| 79 | + return new InternalRange(range.start.line + 1, range.start.character + 1, range.end.line + 1, range.end.character + 1); |
| 80 | +} |
| 81 | + |
| 82 | +enum PromptFileVariableKind { |
| 83 | + Instruction = 'vscode.prompt.instructions.root', |
| 84 | + InstructionReference = `vscode.prompt.instructions`, |
| 85 | + PromptFile = 'vscode.prompt.file' |
| 86 | +} |
| 87 | + |
| 88 | +namespace DiagnosticTagConverter { |
| 89 | + |
| 90 | + /** |
| 91 | + * Additional metadata about the type of a diagnostic. |
| 92 | + */ |
| 93 | + export enum DiagnosticTag { |
| 94 | + /** |
| 95 | + * Unused or unnecessary code. |
| 96 | + * |
| 97 | + * Diagnostics with this tag are rendered faded out. The amount of fading |
| 98 | + * is controlled by the `"editorUnnecessaryCode.opacity"` theme color. For |
| 99 | + * example, `"editorUnnecessaryCode.opacity": "#000000c0"` will render the |
| 100 | + * code with 75% opacity. For high contrast themes, use the |
| 101 | + * `"editorUnnecessaryCode.border"` theme color to underline unnecessary code |
| 102 | + * instead of fading it out. |
| 103 | + */ |
| 104 | + Unnecessary = 1, |
| 105 | + |
| 106 | + /** |
| 107 | + * Deprecated or obsolete code. |
| 108 | + * |
| 109 | + * Diagnostics with this tag are rendered with a strike through. |
| 110 | + */ |
| 111 | + Deprecated = 2, |
| 112 | + } |
| 113 | + export const enum MarkerTag { |
| 114 | + Unnecessary = 1, |
| 115 | + Deprecated = 2 |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + export function from(value: DiagnosticTag) { |
| 120 | + |
| 121 | + switch (value) { |
| 122 | + case DiagnosticTag.Unnecessary: |
| 123 | + return MarkerTag.Unnecessary; |
| 124 | + case DiagnosticTag.Deprecated: |
| 125 | + return MarkerTag.Deprecated; |
| 126 | + default: |
| 127 | + return undefined; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +namespace IDiagnosticVariableEntryFilterData { |
| 134 | + export const icon = Codicon.error; |
| 135 | + |
| 136 | + export function fromMarker(marker: Record<string, unknown>) { |
| 137 | + return { |
| 138 | + filterUri: marker.resource, |
| 139 | + owner: marker.owner, |
| 140 | + problemMessage: marker.message, |
| 141 | + filterRange: { startLineNumber: marker.startLineNumber, endLineNumber: marker.endLineNumber, startColumn: marker.startColumn, endColumn: marker.endColumn } |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + export function toEntry(data: Record<string, unknown>) { |
| 146 | + return { |
| 147 | + id: id(data), |
| 148 | + name: label(data), |
| 149 | + icon, |
| 150 | + value: data, |
| 151 | + kind: 'diagnostic', |
| 152 | + ...data, |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + export function id(data: Record<string, unknown> & { filterRange?: InternalRange }) { |
| 157 | + return [data.filterUri, data.owner, data.filterSeverity, data.filterRange?.startLineNumber, data.filterRange?.startColumn].join(':'); |
| 158 | + } |
| 159 | + |
| 160 | + export function label(data: Record<string, unknown> & { problemMessage?: string; filterUri?: Uri }) { |
| 161 | + const enum TrimThreshold { |
| 162 | + MaxChars = 30, |
| 163 | + MaxSpaceLookback = 10, |
| 164 | + } |
| 165 | + if (data.problemMessage) { |
| 166 | + if (data.problemMessage.length < TrimThreshold.MaxChars) { |
| 167 | + return data.problemMessage; |
| 168 | + } |
| 169 | + |
| 170 | + // Trim the message, on a space if it would not lose too much |
| 171 | + // data (MaxSpaceLookback) or just blindly otherwise. |
| 172 | + const lastSpace = data.problemMessage.lastIndexOf(' ', TrimThreshold.MaxChars); |
| 173 | + if (lastSpace === -1 || lastSpace + TrimThreshold.MaxSpaceLookback < TrimThreshold.MaxChars) { |
| 174 | + return data.problemMessage.substring(0, TrimThreshold.MaxChars) + '…'; |
| 175 | + } |
| 176 | + return data.problemMessage.substring(0, lastSpace) + '…'; |
| 177 | + } |
| 178 | + let labelStr = l10n.t("All Problems"); |
| 179 | + if (data.filterUri) { |
| 180 | + labelStr = l10n.t("Problems in {0}", basename(data.filterUri)); |
| 181 | + } |
| 182 | + |
| 183 | + return labelStr; |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +namespace DiagnosticConverter { |
| 188 | + export function from(value: Diagnostic) { |
| 189 | + let code: string | { value: string; target: Uri } | undefined; |
| 190 | + |
| 191 | + if (value.code) { |
| 192 | + if (isString(value.code) || isNumber(value.code)) { |
| 193 | + code = String(value.code); |
| 194 | + } else { |
| 195 | + code = { |
| 196 | + value: String(value.code.value), |
| 197 | + target: value.code.target, |
| 198 | + }; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return { |
| 203 | + ...toInternalRange(value.range), |
| 204 | + message: value.message, |
| 205 | + source: value.source, |
| 206 | + code, |
| 207 | + severity: DiagnosticSeverityConveter.from(value.severity), |
| 208 | + relatedInformation: value.relatedInformation && value.relatedInformation.map(DiagnosticRelatedInformationConverter.from), |
| 209 | + tags: Array.isArray(value.tags) ? coalesce(value.tags.map(DiagnosticTagConverter.from)) : undefined, |
| 210 | + }; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +namespace DiagnosticRelatedInformationConverter { |
| 215 | + export function from(value: DiagnosticRelatedInformation) { |
| 216 | + return { |
| 217 | + ...toInternalRange(value.location.range), |
| 218 | + message: value.message, |
| 219 | + resource: value.location.uri |
| 220 | + }; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +namespace DiagnosticSeverityConveter { |
| 225 | + export enum MarkerSeverity { |
| 226 | + Hint = 1, |
| 227 | + Info = 2, |
| 228 | + Warning = 4, |
| 229 | + Error = 8, |
| 230 | + } |
| 231 | + |
| 232 | + export function from(value: number): MarkerSeverity { |
| 233 | + switch (value) { |
| 234 | + case DiagnosticSeverity.Error: |
| 235 | + return MarkerSeverity.Error; |
| 236 | + case DiagnosticSeverity.Warning: |
| 237 | + return MarkerSeverity.Warning; |
| 238 | + case DiagnosticSeverity.Information: |
| 239 | + return MarkerSeverity.Info; |
| 240 | + case DiagnosticSeverity.Hint: |
| 241 | + return MarkerSeverity.Hint; |
| 242 | + } |
| 243 | + return MarkerSeverity.Error; |
| 244 | + } |
| 245 | +} |
0 commit comments