• 
      

    Make code line numbers work as links

    Review Request #5576 — Created March 3, 2014 and submitted

    Information

    Review Board
    master
    0ed8ba2...

    Reviewers

    I added two functions to the file, one adds the links to the code line numbers and the another one removes the link when the code line number isn't hovered. The link is combined from the file id and the line number, which should be enough to identify the row, which should be highlighted. The link is removed from the th element, when user hovers on another row. I'm not sure if this is ideal solution, but otherwise the link keeps flicking every time the cursor moves even a bit.
    The hackpad link -> (https://reviewboard.hackpad.com/Diffviewer-Line-Link-Project-Spec-jPwinbJva3E).

    Testing is done on my development server. There is also two screen shots below to demonstrate, how the link is presented.


    Description From Last Updated

    Should the ghostflag show also here, when the code line link is generated? Though the ghostflag will show up, if …

    AU Audore

    We use LessCSS, which allows you to nest rules in some convenient ways. This code can be rewritten as: a …

    chipx86chipx86

    Several issues with style that you'll need to fix up for all of your code: 1) Braces always go on …

    chipx86chipx86

    You should only ever have one var statement, and it should be at the very top of the function. In …

    chipx86chipx86

    You'll want to use this.getLineNum. You also have a lot of code in here doing things that aren't necessary. For …

    chipx86chipx86

    Looks into more of our functions and variables available in this class, like _getActualLineNumCell. You can reuse some of what …

    chipx86chipx86

    Make sure you're using single-quote strings and not double-quote wherever possible.

    chipx86chipx86

    Spaces around +.

    chipx86chipx86

    You're building this string in a couple places. I can't really tell why. Can you explain what that's doing? You …

    chipx86chipx86

    You need to be sure you're indenting when a statement spans multiple lines. Also, you should never need to call …

    chipx86chipx86

    Blank line before this.

    chipx86chipx86

    Careful about trailing whitespace. Go through your change and clean those up.

    chipx86chipx86

    These blank lines should go away.

    chipx86chipx86

    Similar comments as above.

    chipx86chipx86

    Blank line between these.

    chipx86chipx86

    I'm having trouble to implement this. This works fine if the diff is only one lined, but if there's two …

    AU Audore

    You don't need to recurse like this. Just do: $row.find('.copylink')

    chipx86chipx86

    Get rid of these blank lines.

    chipx86chipx86

    I'm having trouble to implement this. This works fine if the diff is only one lined, but if there's two …

    AU Audore

    Here I'm trying to declare the variables used in templates.

    AU Audore

    Here i'm trying to bind the variables to the template. It's not working..

    AU Audore

    Here I try to use the template, so I woudn't pollute it with the same dom code over and over …

    AU Audore

    I think your bug is that you're not passing linkToRow into this invocation of the template: ```javascript this._$link = $(this.linkTemplate({ …

    daviddavid

    This isn't needed.

    chipx86chipx86

    I want to see this like other templates: <th> <a href .....> </th>

    chipx86chipx86

    These names are pretty generic and specific to the link code. Maybe oldLinkLineNum and oldLinkRow?

    chipx86chipx86

    Need a space after the comma.

    chipx86chipx86

    One variable per line.

    chipx86chipx86

    You still have an extra space before the ===

    chipx86chipx86

    Blank line between these.

    chipx86chipx86

    Doing $myel[0].id is faster than $myel.attr('id').

    chipx86chipx86

    Multi-line comments must be in the form of: /* * First line * Second line * ... */ Same with …

    chipx86chipx86

    You're querying 'th' all over the place, in multiple ways. Each time you query, it has to do some expensive …

    chipx86chipx86

    Same with the comment flags.

    chipx86chipx86

    As per my prior comments in other reviews, you don't need to be using .first() and .last() almost ever. Make …

    chipx86chipx86

    Indentation when chaining should be like: $row.children('th') .replaceWith(this.linkTemplate({ ... })); This applies all over the place.

    chipx86chipx86

    What's the difference between this and the ones above? Some extra, clear commenting would help a lot with the complexity …

    chipx86chipx86

    No blank line here.

    chipx86chipx86

    You have an extra space before the )

    chipx86chipx86

    Blank line between these.

    chipx86chipx86

    We only want one var statement.

    chipx86chipx86

    Again, you're querying for stuff too many times. You need to condense these all down.

    chipx86chipx86

    This is already a <th>. You can just do: $row.find('th').text(lineNum); Same elsewhere.

    chipx86chipx86

    Don't add random blank lines in the file. You should go through and remove the unrelated code changes.

    chipx86chipx86

    Why did you do this? Those lines were very important. I can't imagine things are working without them.

    chipx86chipx86

    Please put a blank line between these two statements.

    daviddavid

    Please put a blank line between these two statements.

    daviddavid

    I think the alignment here will be easier to do cleanly if you break it into two statements. How about …

    daviddavid

    There should be a space after the ":" in object literals.

    daviddavid

    Typo in "exist". Also, please add a space after the '.'

    daviddavid

    Please add a space after the '.'

    daviddavid

    Can you put the })); on the next line?

    daviddavid

    Can you separate these with a blank line?

    daviddavid

    Can you put the })); on the next line?

    daviddavid

    Same comments as in the previous method.

    daviddavid

    Can you add a blank line here?

    daviddavid

    After trying this out, I think that having the link show in underlined blue is very distracting, especially in cases …

    daviddavid

    These two variables aren't used--you can get rid of them.

    daviddavid

    In testing this, I noticed that mousing over the new link would de-highlight the row. The issue is that this …

    daviddavid

    Because we're now expecting people to use the right button in the line number rows, this code should be wrapped …

    daviddavid
    AU
    mike_conley
    1. "Should the code line numbers apperance be changed, when it contains the link?"

      Yes - I think we should style it like a standard link - blue, with an underline. That'll do for now, anyways - we can tweak if necessary.

      1. Ok, I'll change the style to that. I have problem with the code line number after I put the link there, it doesn't register mouseOn events for line numbers, so it wont show the row is selected. Should I add a custom mouseOn event for the <a> link or create totally new Backbone view for the links and add the mouseOn there?

    2. 
        
    AU
    AU
    AU
    AU
    1. 
        
    2. Show all issues

      Should the ghostflag show also here, when the code line link is generated? Though the ghostflag will show up, if you aren't pointing the code line number.

    3. 
        
    chipx86
    1. 
        
    2. reviewboard/static/rb/css/diffviewer.less (Diff revision 3)
       
       
       
       
       
       
       
       
       
      Show all issues

      We use LessCSS, which allows you to nest rules in some convenient ways. This code can be rewritten as:

      a {
          color: black;
          text-decoration: none;
      
          &.copylink {
              ...
          }
      }
      
    3. Show all issues

      Several issues with style that you'll need to fix up for all of your code:

      1) Braces always go on the previous line, like: if (...) {

      2) Space after the if

      3) Be careful about extra spaces in the code (you have two before the ==)

      4) In JavaScript, you almost always want to compare using === and !==, not == and !=.

    4. reviewboard/static/rb/js/views/textCommentRowSelector.js (Diff revision 3)
       
       
       
       
       
       
       
      Show all issues

      You should only ever have one var statement, and it should be at the very top of the function. In JavaScript, you want to pre-declare all your variables (since internally, JavaScript will try to do that for you, which can lead to bugs).

    5. Show all issues

      You'll want to use this.getLineNum.

      You also have a lot of code in here doing things that aren't necessary. For example, you should never have to call .filter(function() { ... }) for anything, and you likely don't need to call .first() for anything here. Or .contents().

    6. Show all issues

      Looks into more of our functions and variables available in this class, like _getActualLineNumCell. You can reuse some of what we've already built.

      Also, note that you can call .parents('table') and similar to get a specific parent without having to go up each level manually.

    7. Show all issues

      Make sure you're using single-quote strings and not double-quote wherever possible.

    8. Show all issues

      Spaces around +.

    9. reviewboard/static/rb/js/views/textCommentRowSelector.js (Diff revision 3)
       
       
       
       
       
       
      Show all issues

      You're building this string in a couple places. I can't really tell why. Can you explain what that's doing?

      You also don't want to build this by hand here. Look at how we do templates in JavaScript (search for _.template). You'll want to create one for this and pass in the variables it'll use to build the entire string.

      1. I'm doing something wrong, because I can't get the template to work. If I put some variable inside the template and introduce it in the beginning, I still get reference error: <variable> not defined.

    10. Show all issues

      You need to be sure you're indenting when a statement spans multiple lines.

      Also, you should never need to call .contents() or .filter.

    11. Show all issues

      Blank line before this.

    12. Show all issues

      Careful about trailing whitespace. Go through your change and clean those up.

    13. reviewboard/static/rb/js/views/textCommentRowSelector.js (Diff revision 3)
       
       
       
       
       
       
      Show all issues

      These blank lines should go away.

    14. Show all issues

      Similar comments as above.

    15. Show all issues

      Blank line between these.

    16. Show all issues

      You don't need to recurse like this. Just do: $row.find('.copylink')

    17. reviewboard/static/rb/js/views/textCommentRowSelector.js (Diff revision 3)
       
       
       
       
       
       
       
      Show all issues

      Get rid of these blank lines.

    18. 
        
    AU
    AU
    1. 
        
    2. Show all issues

      I'm having trouble to implement this. This works fine if the diff is only one lined, but if there's two lines and commentflags, getting the first/second (the one which isn't in tr attribute line) line number gets really difficult.

      1. So I'm not entirely sure what the issue is here. This should just be a matter of traversing the DOM to find the right node. Can you extract the relevant block of HTML and pastie it up somewhere?

        Every line has a number, either on the left-hand side or in the center. We should let people copy links via either one.

    3. reviewboard/static/rb/js/views/textCommentRowSelector.js (Diff revisions 3 - 4)
       
       
       
       
       
      Show all issues

      I'm having trouble to implement this. This works fine if the diff is only one lined, but if there's two lines and commentflags, getting the first/second (the one which isn't in tr attribute line) line number gets really difficult.

    4. 
        
    AU
    david
    1. 
        
    2. Show all issues

      I think your bug is that you're not passing linkToRow into this invocation of the template:

      ```javascript
      this._$link = $(this.linkTemplate({
      linkToRow: ...
      }));

    3. 
        
    AU
    1. 
        
    2. Show all issues

      Here I'm trying to declare the variables used in templates.

    3. Show all issues

      Here i'm trying to bind the variables to the template. It's not working..

    4. Show all issues

      Here I try to use the template, so I woudn't pollute it with the same dom code over and over again.

    5. 
        
    AU
    AU
    AU
    AU
    chipx86
    1. The biggest thing distracting me as I read this change is that your code is wildly inconsistent with the rest of the file, in terms of code style. When working on an existing codebase, it is very important that you make sure you are completely consistent. It should be impossible to tell what code you wrote on vs. what code was already there.

      That includes things like:

      • Number of spaces used for indentation (4)
      • Make sure there's a space after if and for (no if())
      • Location of opening braces
      • Location of ending braces
      • One variable per line on var declarations
      • Line lengths (< 80 columns)
      • Where blank lines go (also important not to randomly add or remove blank lines in unrelated parts of the file)
      • Spaces after commas

      Go through and make sure your code completely fits in with our code, and make sure that every single line you write from here on out also fits in. This is going to be an extremely important skill in the industry, where consistency is almost always required.

    2. 
        
    chipx86
    1. Nearly all the comments mentioned here apply in more than one place in the file. Make sure you go through in detail.

    2. Show all issues

      This isn't needed.

    3. Show all issues

      I want to see this like other templates:

      <th>
       <a href .....>
      </th>
      
    4. Show all issues

      These names are pretty generic and specific to the link code. Maybe oldLinkLineNum and oldLinkRow?

    5. Show all issues

      Need a space after the comma.

    6. Show all issues

      One variable per line.

    7. Show all issues

      You still have an extra space before the ===

    8. Show all issues

      Blank line between these.

    9. Show all issues

      Doing $myel[0].id is faster than $myel.attr('id').

    10. Show all issues

      Multi-line comments must be in the form of:

      /*
       * First line
       * Second line
       * ...
       */
      

      Same with your other multi-line comments.

    11. Show all issues

      You're querying 'th' all over the place, in multiple ways. Each time you query, it has to do some expensive lookups. Make sure you only query once per thing you're trying to get.

      1. So can I still check if the diff has two columns this way, or is there a better way to check if the diff has two columns?

    12. Show all issues

      Same with the comment flags.

    13. Show all issues

      As per my prior comments in other reviews, you don't need to be using .first() and .last() almost ever. Make sure it's actually required when you use it.

      1. How do I replace those first() and last(), if my idea was to get the first columns linenumber, which might be different than the second columns linenumber?
        I also use those to insert the correct code linenumber back to either the first column or the second column, if the diff has two columns. What is the better way to do those inserts, if there is two column diff?

    14. Show all issues

      Indentation when chaining should be like:

      $row.children('th')
          .replaceWith(this.linkTemplate({
              ...
          }));
      

      This applies all over the place.

    15. Show all issues

      What's the difference between this and the ones above?

      Some extra, clear commenting would help a lot with the complexity of all this.

      (I know English isn't your first language, but if you have any friends who are proficient, you can ask them to proof-read. Or come to us in IRC.)

    16. Show all issues

      No blank line here.

    17. Show all issues

      You have an extra space before the )

    18. Show all issues

      Blank line between these.

    19. Show all issues

      We only want one var statement.

    20. Show all issues

      Again, you're querying for stuff too many times. You need to condense these all down.

    21. Show all issues

      This is already a <th>. You can just do:

      $row.find('th').text(lineNum);
      

      Same elsewhere.

    22. Show all issues

      Don't add random blank lines in the file. You should go through and remove the unrelated code changes.

    23. Show all issues

      Why did you do this? Those lines were very important. I can't imagine things are working without them.

    24. 
        
    AU
    AU
    AU
    AU
    david
    1. 
        
    2. Show all issues

      Please put a blank line between these two statements.

    3. Show all issues

      Please put a blank line between these two statements.

    4. reviewboard/static/rb/js/views/textCommentRowSelector.js (Diff revision 12)
       
       
       
       
       
       
       
       
       
       
       
       
       
      Show all issues

      I think the alignment here will be easier to do cleanly if you break it into two statements. How about this:

      ```javascript
      this.$link = $(this.linkTemplate({
      ...
      }));
      this.
      $link
      .on({
      ...
      })
      .hide()
      .appendTo('body');

    5. Show all issues

      There should be a space after the ":" in object literals.

    6. Show all issues

      Typo in "exist". Also, please add a space after the '.'

    7. Show all issues

      Please add a space after the '.'

    8. Show all issues

      Can you put the })); on the next line?

    9. Show all issues

      Can you separate these with a blank line?

    10. Show all issues

      Can you put the })); on the next line?

    11. reviewboard/static/rb/js/views/textCommentRowSelector.js (Diff revision 12)
       
       
       
       
       
       
       
      Show all issues

      Same comments as in the previous method.

    12. Show all issues

      Can you add a blank line here?

    13. 
        
    AU
    david
    1. 
        
    2. reviewboard/static/rb/css/diffviewer.less (Diff revision 13)
       
       
       
       
       
      Show all issues

      After trying this out, I think that having the link show in underlined blue is very distracting, especially in cases where the event handling doesn't trigger the mouseout. I vote that we get rid of this and leave the appearance as-is.

    3. Show all issues

      These two variables aren't used--you can get rid of them.

    4. Show all issues

      In testing this, I noticed that mousing over the new link would de-highlight the row. The issue is that this code isn't properly accounting for the new <a> element.

      If you add something like this at the beginning of this method, it should cope with it:

      // Navigate up out of <a class="copylink"> elements
      if ($node.hasClass('copylink')) {
          $node = $node.parent();
      }
      
      ...
      
    5. reviewboard/static/rb/js/views/textCommentRowSelector.js (Diff revision 13)
       
       
       
       
       
       
       
       
       
      Show all issues

      Because we're now expecting people to use the right button in the line number rows, this code should be wrapped inside if (e.button === 1) { ... }

      1. I changed this to e.button === 0, because I tested this out and the 0 was right click.

    6. 
        
    AU
    david
    1. This looks pretty good to me. I'm going to hold off on pushing it until the other part is ready, because opening up the links doesn't work yet. Great work!

    2. 
        
    AU
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to master (a9d5090)