@@ -13,46 +13,56 @@ import type { TutorialStep } from "./types/tutorial.types";
1313 * @param editor The editor to check.
1414 * @param hatTokenMap The hat token map to use for checking if hats are correct.
1515 * @param step The tutorial step whose prerequesites are to be checked.
16- * @returns `true` if the preconditions are met, `false` otherwise.
16+ * @returns A description of the first failed precondition, or `undefined` if
17+ * all preconditions are met.
1718 */
1819export async function arePreconditionsMet (
1920 activeTextEditor : TextEditor | undefined ,
2021 editor : TextEditor | undefined ,
2122 hatTokenMap : HatTokenMap ,
2223 { initialState : snapshot , languageId } : TutorialStep ,
23- ) : Promise < boolean > {
24+ ) : Promise < string | undefined > {
2425 if ( snapshot == null ) {
25- return true ;
26+ return undefined ;
2627 }
2728
2829 if ( activeTextEditor !== editor ) {
29- return false ;
30+ return `Active editor differs from tutorial editor` ;
3031 }
3132
32- if ( editor == null || editor . document . languageId !== languageId ) {
33- return false ;
33+ if ( editor == null ) {
34+ return "Tutorial editor is unavailable" ;
35+ }
36+
37+ if ( editor . document . languageId !== languageId ) {
38+ return `Language differs (expected: ${ languageId } , actual: ${ editor . document . languageId } )` ;
3439 }
3540
3641 if ( editor . document . getText ( ) !== snapshot . documentContents ) {
37- return false ;
42+ return "Document contents differ" ;
3843 }
3944
40- if (
41- ! isEqual ( editor . selections , snapshot . selections . map ( plainObjectToSelection ) )
42- ) {
43- return false ;
45+ const expectedSelections = snapshot . selections . map ( plainObjectToSelection ) ;
46+ if ( ! isEqual ( editor . selections , expectedSelections ) ) {
47+ const expected = expectedSelections
48+ . map ( ( selection ) => selection . concise ( ) )
49+ . join ( ", " ) ;
50+ const actual = editor . selections
51+ . map ( ( selection ) => selection . concise ( ) )
52+ . join ( ", " ) ;
53+ return `Selections differ (expected: ${ expected } , actual: ${ actual } )` ;
4454 }
4555
4656 const readableHatMap = await hatTokenMap . getReadableMap ( false ) ;
4757 for ( const mark of serializedMarksToTokenHats ( snapshot . marks , editor ) ) {
48- if (
49- ! readableHatMap
50- . getToken ( mark . hatStyle , mark . grapheme )
51- ?. range . isRangeEqual ( mark . hatRange )
52- ) {
53- return false ;
58+ const actualRange = readableHatMap . getToken (
59+ mark . hatStyle ,
60+ mark . grapheme ,
61+ ) ?. range ;
62+ if ( ! actualRange ?. isRangeEqual ( mark . hatRange ) ) {
63+ return `Hat ${ mark . hatStyle } . ${ mark . grapheme } differs (expected: ${ mark . hatRange . concise ( ) } , actual: ${ actualRange ?. concise ( ) ?? "none" } )` ;
5464 }
5565 }
5666
57- return true ;
67+ return undefined ;
5868}
0 commit comments