Page MenuHomePhabricator

Add a method to get the indentation for an XHPASTNode
ClosedPublic

Authored by joshuaspence on Apr 5 2015, 11:19 AM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Apr 29, 5:00 PM
Unknown Object (File)
Sun, Apr 28, 3:35 AM
Unknown Object (File)
Thu, Apr 25, 10:55 PM
Unknown Object (File)
Thu, Apr 25, 10:55 PM
Unknown Object (File)
Thu, Apr 25, 10:55 PM
Unknown Object (File)
Thu, Apr 25, 5:12 PM
Unknown Object (File)
Sun, Apr 21, 6:13 PM
Unknown Object (File)
Fri, Apr 19, 2:16 AM
Subscribers

Details

Summary

Add a getIndentation() method to XHPASTNode. This method returns the whitespace that was used to indent the node. This is useful for linter rules which enforce various alignment-related nitpicks, such as D11504: Add a linter rule for PHP indentation and D12296: Improve array comma rule.

Test Plan

See D12296.

Diff Detail

Repository
rPHU libphutil
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

joshuaspence retitled this revision from to Add a `getIndentation` method.
joshuaspence updated this object.
joshuaspence edited the test plan for this revision. (Show Details)
joshuaspence added a reviewer: epriestley.

This seems like it would get the wrong result for:

$a;
        <-- Some trailing whitespace here on this line.
  $b;

Specifically, I would expect the str_replace() at the end to start with an input like <space><space>\n<space><space>, then remove the \n and incorrectly return $b as indented 4 spaces.

Am I misreading?

Right, I guess I am not correctly handling the case in which the whitespace contains more than one newline character.

I'm thinking that I should write some unit tests here

Fix line length. This looks sorta ugly now.

OK. This seems to work better now. I tested this using D12296 on the following file:

<?php

function foo() {
  $x = array(
    1,
    2,
    3);
       
  $y = array(
    1,
    2,
    3);
  
  $z = 
      array(
    1,
    2,
    3);
}

The output (after applying autofixes) was as follows:

<?php

function foo() {
  $x = array(
    1,
    2,
    3,
  );

  $y = array(
    1,
    2,
    3,
  );

  $z =
      array(
    1,
    2,
    3,
      );
}
joshuaspence retitled this revision from Add a `getIndentation` method to Add a method to get the indentation for an XHPASTNode.Apr 5 2015, 1:04 PM
joshuaspence updated this object.
joshuaspence edited the test plan for this revision. (Show Details)

I sort of wonder if this should return something else that makes it easier to patch, but let me look at D12296...

epriestley edited edge metadata.

Ah, irrelevant for D12296.

My thinking here is that if we eventually write a lint rule which adjusts indentation, it couldn't original/replacement a single token because this method may split tokens apart. But I'm not really sure what would be better to return without being a total mess, and such a rule might reasonably use a more top-down approach to analyzing the entire file.

This revision is now accepted and ready to land.Apr 6 2015, 1:26 PM

Ah, irrelevant for D12296.

My thinking here is that if we eventually write a lint rule which adjusts indentation, it couldn't original/replacement a single token because this method may split tokens apart. But I'm not really sure what would be better to return without being a total mess, and such a rule might reasonably use a more top-down approach to analyzing the entire file.

I'm not sure that I follow... this method is only dealing with a single token (although it does destroy the original value of the token, is that your concern?).

This revision was automatically updated to reflect the committed changes.