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

Fixed the bug of tuple and add more test case. #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/kotlinc.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries
.idea/misc.xml
.idea/rholang-idea.iml

# CMake
cmake-build-debug/
Expand Down
11 changes: 6 additions & 5 deletions src/coop/rchain/lang/Rho.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Name_ ::= "_" | NameVar | AT Proc12 {
implements = [ "coop.rchain.lang.psi.RhoNamedElement" ]
mixin = "coop.rchain.lang.psi.RhoNamedElementImpl"
}
RhoName ::= Name_ ("," Name_)*
RhoName ::= Name_ (COMMA Name_)*

// Bundle
Bundle ::= "bundle+" | "bundle-" | "bundle0" | "bundle"
Expand All @@ -188,7 +188,7 @@ Case ::= Case_+

// Name Declarations
// Eventually will have IOPairs.
NameDecl ::= NameDecl_ ("," NameDecl_)*
NameDecl ::= NameDecl_ (COMMA NameDecl_)*
NameDecl_ ::= NameVar OPEN_PAREN URI CLOSE_PAREN | NameVar

// Booleans:
Expand All @@ -199,14 +199,15 @@ Ground ::= Bool | Integer | StringLit | URI

// Collections:
private CollectList ::= OPEN_SQUARE_BRACKET [Proc] ProcRemainder CLOSE_SQUARE_BRACKET
private CollectTuple ::= Tuple
private CollectSet ::= "Set" "(" [Proc] ")"
private CollectMap ::= "{" [KeyValuePair] "}"

// we use Proc_ here instead of Proc to avoid confusion of Grammar kit
private CollectTuple ::= OPEN_PAREN Proc_ (COMMA Proc_)+ CLOSE_PAREN | OPEN_PAREN Proc ",)"
CollectMap ::= "{" [KeyValuePair] "}"

// important: we use Proc_ here, because if we use Proc, they will all consume COMMA
private KeyValuePair_ ::= Proc COLON Proc_
KeyValuePair ::= KeyValuePair_ (COMMA KeyValuePair_)*
private Tuple ::= OPEN_PAREN Proc ",)" | OPEN_PAREN Proc COMMA [Proc] CLOSE_PAREN



Expand Down
13 changes: 3 additions & 10 deletions src/coop/rchain/lang/formatter/RholangSpaceProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ package coop.rchain.lang.formatter
import com.intellij.formatting.Block
import com.intellij.formatting.Spacing
import com.intellij.lang.ASTNode
import com.intellij.openapi.editor.Document
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import com.intellij.psi.formatter.common.AbstractBlock
import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil
import coop.rchain.lang.psi.RhoTypes
import coop.rchain.lang.util.PsiTreeHelpUtil

class RholangSpaceProcessor(private val myNode: ASTNode, private val mySettings: CommonCodeStyleSettings) {

Expand All @@ -20,27 +14,26 @@ class RholangSpaceProcessor(private val myNode: ASTNode, private val mySettings:
return null
}

val elementType = myNode.elementType
val parentType = if (myNode.treeParent == null) null else myNode.treeParent.elementType
val node1 = child1.node
val type1 = node1.elementType
val node2 = child2.node
val type2 = node2.elementType

if (type1 === RhoTypes.OPEN_BRACE && (type2 === RhoTypes.PROC || type2 === RhoTypes.CASE)) {
if(parentType !== RhoTypes.NAME_) {
if (parentType !== RhoTypes.NAME_) {
return Spacing.createSpacing(1, 1, 1, false, 0)
}
}
if (type1 === RhoTypes.BITWISE_OR && myNode.treeParent?.treeParent?.elementType !== RhoTypes.NAME_) {
return Spacing.createSpacing(1, 1, 1, false, 0)
}

if(type1 === RhoTypes.CASE_ || type2 === RhoTypes.CASE_){
if (type1 === RhoTypes.CASE_ || type2 === RhoTypes.CASE_) {
return Spacing.createSpacing(1, 1, 1, false, 0)
}

if(type2 === RhoTypes.CLOSE_BRACE && parentType === RhoTypes.NAME_){
if (type2 === RhoTypes.CLOSE_BRACE && (parentType === RhoTypes.NAME_ || myNode.elementType === RhoTypes.COLLECT_MAP)) {
return null
}

Expand Down
35 changes: 21 additions & 14 deletions test/coop/rchain/ide/folding/RholangCodeInsightTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,39 @@ class RholangCodeInsightTest : LightCodeInsightFixtureTestCase() {
}

fun testFolding() {
doTestFolding("blockDocComment")
doTestFolding("procedure")
}

fun testFormatter() {
private fun doFormatterTest(testName: String) {
object : WriteCommandAction.Simple<Any>(project) {
@Throws(Throwable::class)
override fun run() {
myFixture.configureByFiles("./ide/formatter/token.rho")
myFixture.configureByFiles("./ide/formatter/$testName.rho")
CodeStyleManager.getInstance(project).reformatText(myFixture.file,
ContainerUtil.newArrayList<TextRange>(myFixture.file.textRange))
}
}.execute()
myFixture.checkResultByFile("./ide/formatter/tokenFormatted.rho")
myFixture.checkResultByFile("./ide/formatter/${testName}Formatted.rho")
}

fun testFormatter2() {
object : WriteCommandAction.Simple<Any>(project) {
@Throws(Throwable::class)
override fun run() {
myFixture.configureByFiles("./ide/formatter/simpleOne.rho")
CodeStyleManager.getInstance(project).reformatText(myFixture.file,
ContainerUtil.newArrayList<TextRange>(myFixture.file.textRange))
}
}.execute()
myFixture.checkResultByFile("./ide/formatter/simpleOneFormatted.rho")
fun testComplex() {
doFormatterTest("complex")
}

fun testUri() {
doFormatterTest("URI")
}

fun testIfElse() {
doFormatterTest("ifElse")
}

fun testMapMethod(){
doFormatterTest("mapMethods")
}

fun testParens(){
doFormatterTest("parens")
}

private fun doTestFolding(testName: String) {
Expand Down
16 changes: 5 additions & 11 deletions testData/ide/formatter/URIFormatted.rho
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
// A simple Hello World contract
new helloworld in {
contract helloworld( world ) = {
for( msg <- world ) {
print(msg)
}
} |
new world in {
helloworld!(world) |
world!("Hello World")
}
new ack, stdout(`rho:io:stdout`), stdoutAck(`rho:io:stdoutAck`) in {
stdoutAck!("hello, world!", *ack) |
for (_ <- ack) {
stdout!("received")
}
}
132 changes: 66 additions & 66 deletions testData/ide/formatter/complexFormatted.rho
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
new testResult, updateTestResult in {
testResult!(true) |
contract updateTestResult(@bool, return) = {
for(@r <- testResult) {
match [r, bool] {
[true, true] => {
testResult!(true) |
return!(true)
}
_ => {
testResult!(false) |
return!(false)
}
testResult!(true) |
contract updateTestResult(@bool, return) = {
for(@r <- testResult) {
match [r, bool] {
[true, true] => {
testResult!(true) |
return!(true)
}
_ => {
testResult!(false) |
return!(false)
}
}
}
} |
contract @"CoatCheckDemo"(_) = {
new MakeCoatCheck in {
contract MakeCoatCheck(ret) = {
new port, table in {
ret!(*port) |
for(@"new", @arg, ack <= port) {
new ticket in {
ack!(*ticket) |
@{*ticket | *table}!(arg)
}
} |
for(@"get", @arg, ack <= port) {
for (@value <- @{arg | *table}) {
@{arg | *table}!(value) |
ack!(value)
}
} |
for(@"set", @arg1, @arg2, ack <= port) {
for (_ <- @{arg1 | *table}) {
@{arg1 | *table}!(arg2) |
ack!(true)
}
}
}
} |
contract @"CoatCheckDemo"(_) = {
new MakeCoatCheck in {
contract MakeCoatCheck(ret) = {
new port, table in {
ret!(*port) |
for(@"new", @arg, ack <= port) {
new ticket in {
ack!(*ticket) |
@{*ticket | *table}!(arg)
}
} |
for(@"get", @arg, ack <= port) {
for (@value <- @{arg | *table}) {
@{arg | *table}!(value) |
ack!(value)
}
} |
for(@"set", @arg1, @arg2, ack <= port) {
for (_ <- @{arg1 | *table}) {
@{arg1 | *table}!(arg2) |
ack!(true)
}
}
}
} |
// Usage
new ret, get, set in {
MakeCoatCheck!(*ret) |
for (cc <- ret) {
// Creates new cell with initial value 0
cc!("new", 0, *ret) |
for (ticket <- ret) {
contract get(return) = {
cc!("get", *ticket, *return)
} |
contract set(@value, return) = {
cc!("set", *ticket, value, *return)
} |
// Usage
new ret, get, set in {
MakeCoatCheck!(*ret) |
for (cc <- ret) {
// Creates new cell with initial value 0
cc!("new", 0, *ret) |
for (ticket <- ret) {
contract get(return) = {
cc!("get", *ticket, *return)
} |
contract set(@value, return) = {
cc!("set", *ticket, value, *return)
} |
get!(*ret) |
for(@r <- ret) {
updateTestResult!(r == 0, *ret) |
for(_ <- ret){
set!(1, *ret) |
for(_ <- ret) {
get!(*ret) |
for(@r <- ret) {
updateTestResult!(r == 1, *ret)
}
}
}
}
}
get!(*ret) |
for(@r <- ret) {
updateTestResult!(r == 0, *ret) |
for(_ <- ret){
set!(1, *ret) |
for(_ <- ret) {
get!(*ret) |
for(@r <- ret) {
updateTestResult!(r == 1, *ret)
}
}
}
}
}
}
} |
@"CoatCheckDemo"!(Nil)
}
}
} |
@"CoatCheckDemo"!(Nil)
}
7 changes: 7 additions & 0 deletions testData/ide/formatter/ifElse.rho
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
new dupe in {
contract dupe(@depth) = {
if (depth <= 0) { Nil } else {
dupe!(depth - 1) | dupe!(depth - 1) | dupe!(depth - 1) | dupe!(depth - 1) | dupe!(depth - 1) | dupe!(depth - 1) | dupe!(depth - 1) | dupe!(depth - 1) | dupe!(depth - 1) | dupe!(depth - 1)
}
} | dupe!(2)
}
19 changes: 19 additions & 0 deletions testData/ide/formatter/ifElseFormatted.rho
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
new dupe in {
contract dupe(@depth) = {
if (depth <= 0) {
Nil
} else {
dupe!(depth - 1) |
dupe!(depth - 1) |
dupe!(depth - 1) |
dupe!(depth - 1) |
dupe!(depth - 1) |
dupe!(depth - 1) |
dupe!(depth - 1) |
dupe!(depth - 1) |
dupe!(depth - 1) |
dupe!(depth - 1)
}
} |
dupe!(2)
}
18 changes: 18 additions & 0 deletions testData/ide/formatter/mapMethods.rho
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// all methods modifying the underlying collection return new Map
// prints 1
new stdout in { stdout!({"one" : 2, "two" : 2, "three" : 3}.get("one")) |
// if element is not present in the map will return Nil -- prints Nil
stdout!({"one" : 1, "two" : 2, "three" : 3}.get("four")) |
// prints false
stdout!({"one" : 1, "two" : 2, "three" : 3}.contains("four")) |
// prints true
stdout!({"one" : 1, "two" : 2, "three" : 3}.contains("three")) |
// prints @{{"three" : 3, "two" : 2}}
stdout!({"one" : 1, "two" : 2, "three" : 3}.delete("one")) |
// doesn't change the collection, prints @{{"one" : 1, "three" : 3, "two" : 2}}
stdout!({"one" : 1, "two" : 2, "three" : 3}.delete("four")) |
// returns new map which is a result of subtracting elements of second map from the base map
stdout!({"one" : 1, "two" : 2, "three" : 3}.diff({"one" : 1, "four": 4})) |
// merges two maps -- returns @{{"four" : 4, "one" : 1, "three" : 3, "two" : 2}}
stdout!({"one" : 1, "two" : 2, "three" : 3}.union({"one" : 1, "four": 4}))
}
6 changes: 3 additions & 3 deletions testData/ide/formatter/mapMethodsFormatted.rho
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// all methods modifying the underlying collection return new Map
// prints 1
new stdout in { stdout!({"one" : 2, "two" : 2, "three" : 3}.get("one")) |
new stdout in {
stdout!({"one" : 2, "two" : 2, "three" : 3}.get("one")) |
// if element is not present in the map will return Nil -- prints Nil
stdout!({
"one" : 1, "two" : 2, "three" : 3}.get("four")) |
stdout!({"one" : 1, "two" : 2, "three" : 3}.get("four")) |
// prints false
stdout!({"one" : 1, "two" : 2, "three" : 3}.contains("four")) |
// prints true
Expand Down
3 changes: 3 additions & 0 deletions testData/ide/formatter/parens.rho
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
new stdout(`rho:io:stdout`) in {stdout!((2,)) | stdout!(2 * (3 + 5)) |
stdout!(1 + 3) | @(3,)!(2 + 4) | stdout!(3 * (1 + (2 / 2)))
}
7 changes: 7 additions & 0 deletions testData/ide/formatter/parensFormatted.rho
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
new stdout(`rho:io:stdout`) in {
stdout!((2,)) |
stdout!(2 * (3 + 5)) |
stdout!(1 + 3) |
@(3,)!(2 + 4) |
stdout!(3 * (1 + (2 / 2)))
}
Loading