@@ -15,6 +15,8 @@ macro_rules! contract_file {
1515
1616const GRAMMAR_PLUGIN : & str = contract_file ! ( "fixtures/languages/language-grammar.plugin.json" ) ;
1717const HTTP_GRAMMAR : & str = contract_file ! ( "fixtures/languages/grammars/http.tmLanguage.json" ) ;
18+ const FIRST_LINE_PLUGIN : & str = contract_file ! ( "fixtures/languages/first-line.plugin.json" ) ;
19+ const SAMPLE_GRAMMAR : & str = contract_file ! ( "fixtures/languages/grammars/sample.tmLanguage.json" ) ;
1820
1921fn manifest ( source : & str ) -> Value {
2022 serde_json:: from_str ( source) . expect ( "fixture manifest is JSON" )
@@ -381,72 +383,62 @@ fn configuration_only_contribution_decodes_without_grammar() {
381383}
382384
383385#[ test]
384- fn first_line_only_language_requires_a_compatible_manifest_and_host ( ) {
385- let mut plugin = manifest ( contract_file ! ( "fixtures/languages/first-line.plugin.json" ) ) ;
386- plugin[ "languages" ] [ 0 ]
387- . as_object_mut ( )
388- . unwrap ( )
389- . remove ( "configuration" ) ;
390- let validate = |plugin : & Value , versions : & [ u32 ] | {
391- validate_command ( json ! ( { "manifest" : plugin, "hostCapabilities" : { "languages" : versions} } ) )
392- . unwrap ( )
393- } ;
394- let outcome = validate ( & plugin, & [ 0 , 1 ] ) ;
386+ fn first_line_grammar_uses_the_existing_language_capability ( ) {
387+ let plugin = manifest ( FIRST_LINE_PLUGIN ) ;
388+ let admission = validate_command ( json ! ( {
389+ "manifest" : plugin,
390+ "hostCapabilities" : { "languages" : [ 0 ] } ,
391+ } ) )
392+ . unwrap ( ) ;
393+ assert_eq ! ( admission[ "status" ] , "ok" , "{admission}" ) ;
394+ let outcome = decode_grammar_command ( json ! ( {
395+ "manifest" : admission[ "plugin" ] ,
396+ "languageID" : "sample" ,
397+ "grammar" : SAMPLE_GRAMMAR ,
398+ } ) )
399+ . unwrap ( ) ;
395400 assert_eq ! ( outcome[ "status" ] , "ok" , "{outcome}" ) ;
401+ let language = & outcome[ "language" ] ;
402+ assert_eq ! ( language[ "fileExtensions" ] , json!( [ "sample" ] ) ) ;
403+ assert_eq ! ( language[ "configuration" ] [ "lineComment" ] , "//" ) ;
404+ assert_eq ! ( language[ "grammar" ] [ "scopeName" ] , "source.sample" ) ;
405+ assert_eq ! ( language[ "grammar" ] [ "source" ] , SAMPLE_GRAMMAR ) ;
396406 assert_eq ! (
397- outcome[ "plugin" ] [ "languages" ] [ 0 ] [ "firstLineMatch" ] ,
398- plugin[ "languages" ] [ 0 ] [ "firstLineMatch" ]
399- ) ;
400-
401- let unsupported = validate ( & plugin, & [ 0 ] ) ;
402- assert_eq ! ( unsupported[ "status" ] , "rejected" ) ;
403- assert_eq ! ( unsupported[ "error" ] [ "field" ] , "requires.languages" ) ;
404- let mut downgraded = plugin;
405- downgraded[ "requires" ] [ "languages" ] = json ! ( 0 ) ;
406- let invalid = validate ( & downgraded, & [ 0 , 1 ] ) ;
407- assert_eq ! (
408- invalid[ "error" ] [ "code" ] , "plugin.manifest.invalid" ,
409- "{invalid}"
407+ language[ "grammar" ] [ "firstLineMatch" ] ,
408+ manifest( SAMPLE_GRAMMAR ) [ "firstLineMatch" ]
410409 ) ;
411- assert_eq ! ( invalid[ "error" ] [ "field" ] , "requires.languages" ) ;
412-
413- let legacy = validate ( & manifest ( GRAMMAR_PLUGIN ) , & [ 0 , 1 ] ) ;
414- assert_eq ! ( legacy[ "status" ] , "ok" , "{legacy}" ) ;
415- assert ! ( legacy[ "plugin" ] [ "languages" ] [ 0 ]
416- . get( "firstLineMatch" )
417- . is_none( ) ) ;
418410}
419411
420412#[ test]
421- fn first_line_languages_reject_invalid_and_unbounded_patterns_at_installation ( ) {
422- let mut plugin = manifest ( contract_file ! ( "fixtures/languages/first-line.plugin.json" ) ) ;
413+ fn unsupported_first_line_metadata_keeps_its_grammar ( ) {
423414 for pattern in [
424- "" . into ( ) ,
425- "a*" . into ( ) ,
426- "[" . into ( ) ,
427- "(a)\\ 1" . into ( ) ,
428- "a{10000000}" . into ( ) ,
429- "é" . repeat ( 257 ) ,
415+ Value :: Null ,
416+ json ! ( 42 ) ,
417+ json ! ( "" ) ,
418+ json ! ( "a*" ) ,
419+ json ! ( "[" ) ,
420+ json ! ( r"(a)\1" ) ,
421+ json ! ( "a{10000000}" ) ,
422+ json ! ( "é" . repeat( 257 ) ) ,
430423 ] {
431- plugin[ "languages" ] [ 0 ] [ "firstLineMatch" ] = json ! ( pattern) ;
432- let outcome = validate_command ( json ! ( { "manifest" : plugin} ) ) . unwrap ( ) ;
433- assert_eq ! (
434- outcome[ "error" ] [ "field" ] , "languages[0].firstLineMatch" ,
435- "{outcome}"
436- ) ;
424+ let mut grammar = grammar_with ( json ! ( [ ] ) ) ;
425+ grammar[ "firstLineMatch" ] = pattern;
426+ let source = grammar. to_string ( ) ;
427+ let outcome = decode_grammar ( & source) ;
428+ assert_eq ! ( outcome[ "status" ] , "ok" , "{outcome}" ) ;
429+ let grammar = & outcome[ "language" ] [ "grammar" ] ;
430+ assert ! ( !grammar. as_object( ) . unwrap( ) . contains_key( "firstLineMatch" ) ) ;
431+ assert_eq ! ( grammar[ "scopeName" ] , "source.test" ) ;
432+ assert_eq ! ( grammar[ "source" ] , source) ;
437433 }
438- plugin[ "languages" ] [ 0 ] [ "firstLineMatch" ] = Value :: Null ;
439- assert_eq ! (
440- validate_command( json!( { "manifest" : plugin} ) ) . unwrap( ) [ "status" ] ,
441- "rejected"
442- ) ;
443434}
444435
445436#[ test]
446437fn first_line_matching_keeps_registered_priority_and_never_reads_past_the_line ( ) {
447- let plugin = manifest ( contract_file ! ( "fixtures/languages/first-line.plugin.json" ) ) ;
448- let specific =
449- json ! ( { "languageID" : "sample" , "firstLineMatch" : plugin[ "languages" ] [ 0 ] [ "firstLineMatch" ] } ) ;
438+ let specific = json ! ( {
439+ "languageID" : "sample" ,
440+ "firstLineMatch" : manifest( SAMPLE_GRAMMAR ) [ "firstLineMatch" ] ,
441+ } ) ;
450442 let generic = json ! ( { "languageID" : "other" , "firstLineMatch" : "#!" } ) ;
451443 let choose = |line : & str , candidates : & [ Value ] | {
452444 let response = lithe_plugin_manifest:: match_first_line (
0 commit comments