question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Troubles with building a syntax file

See original GitHub issue

I’m attempting to write a syntax file for Laravel Blade to contribute, and I am coming up against a wall trying to get single word directives like @continue to highlight, which seems like it should be simple enough.

Additionally there are more complex directives like @for($i = 0; $i < 2; $i++) which I want highlighted and their contents treated like PHP.

I’ve posted a question on SO, but figured it was pretty niche and posting here might help get some eyeballs on it.

https://stackoverflow.com/questions/65364723/having-trouble-capturing-blade-directives-with-highlight-js

And here’s the code I have so far, in case someone doesn’t like external links:

export default function(hljs) {
  const EXPRESSION_DIRECTIVES = {
    $pattern: /@[a-zA-Z]+/,
    'template-tag': [
      'case', 'each', 'elseif', 'empty', 'env', 'error', 'extends', 'for', 'foreach', 'forelse',
      'hasSection', 'if', 'include', 'includeFirst', 'includeIf', 'includeUnless', 'includeWhen',
      'inject', 'isset', 'method', 'prepend', 'section', 'sectionMissing', 'stack', 'switch',
      'unless', 'while', 'yield'
    ].map(v => '@' + v).join(" ")
  };

  const STANDALONE_DIRECTIVES = {
    $pattern: /@[a-zA-Z]+/,
    'template-tag': [
      'auth', 'break', 'continue', 'csrf', 'default', 'else', 'endauth', 'endempty', 'endenv',
      'enderror', 'endfor', 'endforeach', 'endforelse', 'endguest', 'endif', 'endisset', 'endphp',
      'endprepend', 'endproduction', 'endpush', 'endsection', 'endswitch', 'endunless', 'endwhile',
      'guest', 'once', 'parent', 'php', 'production', 'props', 'push', 'show'
    ].map(v => '@' + v).join(" ")
  };

  return {
    name: 'Blade',
    case_insensitive: true,
    subLanguage: 'xml',
    contains: [

      hljs.COMMENT(/\{\{--/, /--\}\}/),

      // output with HTML escaping
      {
        className: 'template-variable',
        begin: /\{\{/,
        starts: {
          end: /\}\}/,
          returnEnd: true,
          subLanguage: 'php',
        },
      },
      {
        className: 'template-variable',
        begin: /\}\}/,
      },

      // output with no HTML escaping
      {
        className: 'template-variable',
        begin: /\{!!/,
        starts: {
          end: /!!\}/,
          returnEnd: true,
          subLanguage: 'php',
        },
      },
      {
        className: 'template-variable',
        begin: /!!\}/,
      },

      // directly inserted PHP code
      {
        className: 'template-tag',
        begin: /@php/,
        starts: {
          end: /@endphp/,
          returnEnd: true,
          subLanguage: 'php',
        },
        relevance: 10
      },

      // directives with arguments like @yield('foo')
      {
        begin: /@([a-zA-Z]+)\s*\(/,
        keywords: EXPRESSION_DIRECTIVES,
        starts: {
          end: /\)/,
          returnEnd: true,
          subLanguage: 'php'
        }
      },

      // directives without arguments like @section
      {
        begin: /@([a-zA-Z]+)/,
        keywords: STANDALONE_DIRECTIVES,
      },

    ]
  };
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
joshgoebelcommented, Dec 19, 2020

Those relevances are pretty scary if you ever plan to participate in auto-detection. Most typically one should not use relevance at all (other than to set it to 0 for many things). The only thing I see here that might deserve a 10 is @php - though @phpend would be far better to give the points too, since it’s longer and more specific. Your real relevance will come from your sublanguages as they match tokens.

Example:

  • @section is a perfectly valid variable in Ruby with a relevance of 1.
  • You score it 20.
1reaction
joshgoebelcommented, Dec 18, 2020

That does not work because @ isn’t in the default $pattern. (so no keyword with @ will ever match) The default is \w+ (word like characters)… so “blah_blah” is a potential keyword, “@blah” is not.

You may want to checkout:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating a syntax file - YouTube
SPSS Syntax files ; how to open them and write notes to yourself within the program.
Read more >
SPSS Tutorials: Using SPSS Syntax - LibGuides
Opening Syntax Files. To open a syntax file on your computer, click File > Open > Syntax. You can do this from any...
Read more >
7.3. Catching Ant Build File Syntax Problems - O'Reilly
You have a problem with a build file, and you would like to track it down. Solution. The current version of Ant displays...
Read more >
A case against syntax highlighting - Linus Åkesson
Syntax highlighting can show you, for example, that you are using library subroutine name as variable. And, in C, this can be a...
Read more >
Problem with the FACTOR command in syntax - Forums - IBM
I currently use SPSS for Mac v21 and v22. I have successfully used SPSS syntax to transform variables and analyze data for many...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found