Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3814 Correct highlighting issues in REPL #3987

Merged
merged 4 commits into from
May 7, 2018

Conversation

benkobalog
Copy link
Contributor

@benkobalog benkobalog commented Feb 12, 2018

Correct highlighting after "|", ":", "&" characters
and after "case", "val", "val" when using extractors.

I added test cases to cover the faulty cases in the ticket, plus some basic cases

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Have an awesome day! ☀️

import scala.io.Source

/** Runs all tests contained in `compiler/test-resources/printing/`
* To check test cases, you can use "cat" or "less -r" from bash*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feature request: add a mode to generate the test files from the syntax highlighter output, this would be useful since you cannot just copy-paste the highlighted output when the test files need to be updated.

Copy link
Contributor Author

@benkobalog benkobalog Feb 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this?

  private def testFile(f: JFile): Unit = {
    val linesIt = Source.fromFile(f).getLines()
    val input = linesIt.takeWhile(_ != "result:").mkString("\n")
    val expectedOutput = linesIt.mkString("\n")
    val actualOutput = SyntaxHighlighting(input).mkString

    if (expectedOutput != actualOutput) {
      println("Expected output:")
      println(expectedOutput)
      println("Actual output:")
      println(actualOutput)

      if (f.getName == "nameOfFileToConvertToATest") generateTestFile()

      fail(s"Error in file $f, expected output did not match actual")
    }

    def generateTestFile(): Unit = {
      val path = "compiler/test-resources/printing/" + f.getName
      new PrintWriter(path) {
        write(input + "\nresult:\n" + actualOutput)
        close()
      }
      println(s"Test file for ${f.getName} has been generated")
    }
  }

Originally I used a simple function which wrote to a file in the project's root folder and copied its content manually after "result:" line.
This generateTestFile function more convenient. However I'm not sure if it needs explaining and if yes how much.
(it works on a plain scala file and on a changed test file as well)

@benkobalog
Copy link
Contributor Author

@smarter Can please take a look?

@smarter
Copy link
Member

smarter commented Feb 27, 2018

Not available this week, maybe @allanrenucci can have a look?

@allanrenucci allanrenucci self-assigned this Feb 27, 2018
Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, sorry for the wait, great to see tests for this!

case _ => false
}

def shouldUpdateLastToken(currentPotentialToken: String): Boolean =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a documentation comment for this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will write documentation to this.
Also I'm thinking about renaming currentPotentialToken to just currentToken. "Potential" wanted to express that it might be the next "lastToken", but now I don't think it adds any value to the name.

benkobalog added 3 commits May 2, 2018 17:55
Correct highlighting after "|", ":", "&" characters
and after "case", "val", "val" when using extractors.
Added comments to updateLastToken function
Use syntax highlight tests proposed in pr scala#4441
instead of the file based solution
@benkobalog
Copy link
Contributor Author

@smarter I added some documentation, but I also refactored the function a bit.

case _ => false
val valDefStarterTokens = "var" :: "val" :: "def" :: "case" :: Nil

/** lastToken is used to check whether we want to show something
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we shouldn't call it lastToken then since it's not the last token anymore :).

}

@Test
def valVarDef = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split the tests below into smaller tests, that that test only one specific thing. E.g:

@Test
def unionTypes = {
  test("type A = String|Int| Long", "<K|type> <T|A> = <T|String>|<T|Int>| <T|Long>")
  test("type B = String |Int| Long", "<K|type> <T|B> = <T|String> |<T|Int>| <T|Long>")
  test("type C = String | Int | Long", "<K|type> <T|C> = <T|String> | <T|Int> | <T|Long>")
  test("type D = String&Int& Long", "<K|type> <T|D> = <T|String>&<T|Int>& <T|Long>")
  test("type E = String &Int& Long", "<K|type> <T|E> = <T|String> &<T|Int>& <T|Long>")
  test("type F = String & Int & Long", "<K|type> <T|F> = <T|String> & <T|Int> & <T|Long>")
  test("fn[String|Char](input)", "fn[<T|String>|<T|Char>](input)")
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that the code snippets don't need to compile. So if for example you want to test syntax highlighting for pattern matching, you don't need to define an ADT for it

@benkobalog
Copy link
Contributor Author

@allanrenucci @smarter Thanks for the feedback! I changed both things you said.

@allanrenucci allanrenucci merged commit c5ef4eb into scala:master May 7, 2018
@allanrenucci
Copy link
Contributor

Thanks @benkobalog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants