GithubHelp home page GithubHelp logo

Comments (5)

skx avatar skx commented on July 22, 2024

As a workaround I've defined a single-line comment as being "--SPACE" rather than --. That works, but feels horrid :)

from kilo.

skx avatar skx commented on July 22, 2024

PS. this requires, obviously:

 char singleline_comment_start[5];
 char multiline_comment_start[5];
 char multiline_comment_end[5];

Because "--[[" and "--]]" are larger than the default of two characters. I suspect the issue here is the handling of comments which assumes 2 characters only, so changing things like this:

    /* Handle // comments. */
    if (prev_sep && *p == scs[0] && *(p+1) == scs[1]) {
        /* From here to end is a comment */
        memset(row->hl+i,HL_COMMENT,row->rsize-i);
        return;
    }

To using strcmp, rather than just testing the first two characters, is going to be needed.

Anyway summary of the bug:

  • If single-character and multi-character share a prefix.
  • And that prefix is two characters in length.
  • Highlighting is broken.

from kilo.

skx avatar skx commented on July 22, 2024

Finally another example where this is broken is if you define a single-line comment prefix as "#", to cover shell-scripts.

Because the assumption is that comment-prefixes are two characters you'll never get the highlighting you want.

from kilo.

skx avatar skx commented on July 22, 2024

I have a patch for this, which will suit vanilla kilo too, but it's waiting on #28 before I can commit it.

from kilo.

skx avatar skx commented on July 22, 2024

The following patch updates to work as expected.

NOTE:

  • I made singleline_comment_start + singleline_comment_end 5 characters to cover Lua which uses "--[[" and --]] respectively (4 chars + trailing null).
  • I updated editorRowHasOpenComment to be recursive.
    • I also added a few more sanity checks there as a result of paranoia when investigating #28 - it could be made more terse.
  • The change of characters in the syntax definition is just an example, not part of the patch.

If this is unclear please see the source commit - Pull request not submitted just yet because I didn't know what to base it on, there are too many outstanding ones :)

 diff --git a/kilo.c b/kilo.c
 index 9490a77..d6817d2 100644
 --- a/kilo.c
 +++ b/kilo.c
 @@ -37,6 +37,7 @@
  #define _BSD_SOURCE
  #define _GNU_SOURCE

 +#include <time.h>
  #include <termios.h>
  #include <stdlib.h>
  #include <stdio.h>
 @@ -68,9 +69,9 @@
      char **filematch;
      char **keywords;
 -    char singleline_comment_start[2];
 -    char multiline_comment_start[3];
 -    char multiline_comment_end[3];
 +    char singleline_comment_start[5];
 +    char multiline_comment_start[5];
 +    char multiline_comment_end[5];
      int flags;
  };

 @@ -176,7 +177,7 @@ struct editorSyntax HLDB[] = {
          /* C / C++ */
          C_HL_extensions,
          C_HL_keywords,
 -        "//","/*","*/",
 +        "-- ","--[[","--]]",
          HL_HIGHLIGHT_STRINGS | HL_HIGHLIGHT_NUMBERS
      }
  };
 @@ -357,10 +358,35 @@ int is_separator(int c) {
   * that starts at this row or at one before, and does not end at the end
   * of the row but spawns to the next row. */
  int editorRowHasOpenComment(erow *row) {
 -    if (row->hl && row->rsize && row->hl[row->rsize-1] == HL_MLCOMMENT &&
 -        (row->rsize < 2 || (row->render[row->rsize-2] != '*' ||
 -                            row->render[row->rsize-1] != '/'))) return 1;
 -    return 0;
 +    /*
 +     * If the line is empty - then we have to check on the line before
 +     * that.
 +     */
 +    if ( row->size == 0  && ( row->idx > 0 ) )
 +        return( editorRowHasOpenComment(&E.row[row->idx-1]));
 +
 +    if ( row->size == 0 )
 +        return 0;
 +
 +    /*
 +     * OK we have some text.  Is the line ending in a MLCOMMENT
 +     * character-string?
 +     */
 +    if (row->hl && row->rsize && row->hl[row->rsize-1] != HL_MLCOMMENT )
 +        return 0;
 +
 +    /*
 +     * OK the line ends in a comment.  Are the closing characters
 +     * our closing tokens though?
 +     */
 +    int len = (int)strlen(E.syntax->multiline_comment_end);
 +    char *end = E.syntax->multiline_comment_end;
 +
 +    if ( strncmp( row->render + row->rsize - len, end, len ) == 0 )
 +        return 0;
 +    else
 +        return 1;
 +
  }

  /* Set every byte of row->hl (that corresponds to every character in the line)
 @@ -374,9 +400,6 @@ void editorUpdateSyntax(erow *row) {
      int i, prev_sep, in_string, in_comment;
      char *p;
      char **keywords = E.syntax->keywords;
 -    char *scs = E.syntax->singleline_comment_start;
 -    char *mcs = E.syntax->multiline_comment_start;
 -    char *mce = E.syntax->multiline_comment_end;

      /* Point to the first non-space char. */
      p = row->render;
 @@ -395,8 +418,8 @@ void editorUpdateSyntax(erow *row) {
          in_comment = 1;

      while(*p) {
 -        /* Handle // comments. */
 -        if (prev_sep && *p == scs[0] && *(p+1) == scs[1]) {
 +        /* Handle // comments - colour the rest of the line and return. */
 +        if (prev_sep && strlen(E.syntax->singleline_comment_start) && ( strncmp( p,E.syntax->singleline_comment_start, strlen(E.syntax->singleline_comment_start)) == 0 ) ) {
              /* From here to end is a comment */
              memset(row->hl+i,HL_COMMENT,row->size-i);
              return;
 @@ -405,9 +428,15 @@ void editorUpdateSyntax(erow *row) {
          /* Handle multi line comments. */
          if (in_comment) {
              row->hl[i] = HL_MLCOMMENT;
 -            if (*p == mce[0] && *(p+1) == mce[1]) {
 -                row->hl[i+1] = HL_MLCOMMENT;
 -                p += 2; i += 2;
 +            if (strncmp(p, E.syntax->multiline_comment_end,
 +                        strlen(E.syntax->multiline_comment_end)) == 0 ) {
 +
 +                for( int x = 0; x < (int)strlen(E.syntax->multiline_comment_end); x++ )
 +                {
 +                    row->hl[i+x] = HL_MLCOMMENT;
 +                }
 +                p += strlen(E.syntax->multiline_comment_end) ;
 +                i += strlen(E.syntax->multiline_comment_end) ;
                  in_comment = 0;
                  prev_sep = 1;
                  continue;
 @@ -416,10 +445,15 @@ void editorUpdateSyntax(erow *row) {
                  p++; i++;
                  continue;
              }
 -        } else if (*p == mcs[0] && *(p+1) == mcs[1]) {
 -            row->hl[i] = HL_MLCOMMENT;
 -            row->hl[i+1] = HL_MLCOMMENT;
 -            p += 2; i += 2;
 +        } else if (strncmp(p, E.syntax->multiline_comment_start,
 +                           strlen(E.syntax->multiline_comment_start) ) == 0 ) {
 +
 +            for (int  x = 0; x < (int)strlen(E.syntax->multiline_comment_start) ; x++ )
 +            {
 +                row->hl[i + x] = HL_MLCOMMENT;
 +            }
 +            p += (int)strlen(E.syntax->multiline_comment_start) ;
 +            i += (int)strlen(E.syntax->multiline_comment_start) ;
              in_comment = 1;
              prev_sep = 0;
              continue;

from kilo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.