Skip to content

Commit

Permalink
ALS-732: Array value suggestions won't force brackets for single valu…
Browse files Browse the repository at this point in the history
…e arrays
  • Loading branch information
llibarona committed Mar 11, 2019
1 parent 0863cf2 commit 06aef68
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class KnownPropertyValuesCompletionPlugin extends ICompletionPlugin {
if (request.astNode.isEmpty) {
false
} else {
var prop = request.astNode.get.property
val prop = request.astNode.get.property
if (request.actualYamlLocation.isEmpty) {
false
} else if (request.kind == LocationKind.KEY_COMPLETION
Expand All @@ -48,7 +48,7 @@ class KnownPropertyValuesCompletionPlugin extends ICompletionPlugin {
parentNode = astNode.parent
} else if (request.actualYamlLocation.get.hasSameValue(request.yamlLocation.get)) {
if (astNode.isElement) {
var valueLocation = request.yamlLocation.get.value.get
val valueLocation = request.yamlLocation.get.value.get
var propName: Option[String] = None
valueLocation.yPart match {
case yMap: YMap =>
Expand Down Expand Up @@ -146,11 +146,11 @@ object KnownPropertyValuesCompletionPlugin {
}
}

val ID = "known.property.values.completion";
val ID = "known.property.values.completion"

val supportedLanguages: List[Vendor] = List(Raml10, Oas, Oas20, Aml);
val supportedLanguages: List[Vendor] = List(Raml10, Oas, Oas20, Aml)

def apply(): KnownPropertyValuesCompletionPlugin = new KnownPropertyValuesCompletionPlugin();
def apply(): KnownPropertyValuesCompletionPlugin = new KnownPropertyValuesCompletionPlugin()

def isSequence(n: IHighLevelNode, pName: String): Boolean = {
var pm = n.astUnit.positionsMapper
Expand All @@ -161,7 +161,7 @@ object KnownPropertyValuesCompletionPlugin {
me.entries.find(x => x.key.isThisKey(pName)) match {
case Some(me) =>
me.value.value match {
case s: YSequence => true
case _: YSequence => true
case _ => false
}
case _ => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ class TemplateReferencesCompletionPlugin extends ICompletionPlugin {
case "type" =>
refYamlKind = templateRefYamlKind(request, owner, "type")
readableName = "Resource type"
Search.getDeclarations(n.astUnit, "ResourceType");
Search.getDeclarations(n.astUnit, "ResourceType")

case "is" =>
squareBracketsRequired = true
refYamlKind = templateRefYamlKind(request, owner, "is")
readableName = "Trait"
Search.getDeclarations(n.astUnit, "Trait");
Search.getDeclarations(n.astUnit, "Trait")

case _ => Seq();
case _ => Seq()
}
declarations = declarations.filter(x => {
val nameOpt = x.node
Expand Down Expand Up @@ -98,12 +98,12 @@ class TemplateReferencesCompletionPlugin extends ICompletionPlugin {
.getOrElse(Nil)
} else {
declarations.map(declaration => {
val ts = toTemplateSuggestion(declaration, refYamlKind.get)
val ts = toTemplateSuggestion(declaration, refYamlKind.get, request.prefix)
Suggestion(ts.get.text, readableName, ts.get.name, request.prefix)
})
}

case _ => Seq();
case _ => Seq()
}

val response = CompletionResponse(result, LocationKind.VALUE_COMPLETION, request)
Expand Down Expand Up @@ -145,17 +145,35 @@ class TemplateReferencesCompletionPlugin extends ICompletionPlugin {

def templateRefYamlKind(request: ICompletionRequest, owner: IHighLevelNode, propName: String): Option[RefYamlKind] = {
request.astNode.flatMap(node => {
var pos = request.position

var openBracket = false

def isEntry(entry: YMapEntry, propName: String): Boolean = {
if (entry.key.value.toString == propName) true
else
entry.value.value.asInstanceOf[YMap].entries.head match {
case h if h.key.value.toString == propName =>
openBracket = true
true
case _ => false
}
}

val pos = request.position
val pm = node.astUnit.positionsMapper
Option(owner).flatMap(pNode => {
val si = pNode.sourceInfo
si.yamlSources.headOption
.filter(_.isInstanceOf[YMapEntry])
.map(_.asInstanceOf[YMapEntry].value.value)
.filter(_.isInstanceOf[YMap])
.flatMap(_.asInstanceOf[YMap].entries.find(e => e.key.value.toString == propName))
.flatMap(_.asInstanceOf[YMap].entries.find(e => isEntry(e, propName)))
.flatMap(propNode => {
var line = YRange(propNode, Option(pm)).start.line
val pNode = openBracket match {
case true if propNode.value.isInstanceOf[YMapEntry] => propNode.value.asInstanceOf[YMapEntry]
case _ => propNode
}
val line = YRange(propNode, Option(pm)).start.line
val lineStr = pm.lineString(line)
val offset = lineStr.map(pm.lineOffset).getOrElse(-1)
propNode.value.value match {
Expand All @@ -173,7 +191,7 @@ class TemplateReferencesCompletionPlugin extends ICompletionPlugin {
wrappedFlow = isFlow(x.value, pm)
}
})
Some(RefYamlKind.sequence(fl, wrappedInMap, wrappedFlow, off))
Some(RefYamlKind.sequence(fl || openBracket, wrappedInMap, wrappedFlow, off))
case map: YMap =>
val fl = isFlow(map, pm)
val off = if (fl) -1 else offset
Expand All @@ -187,15 +205,15 @@ class TemplateReferencesCompletionPlugin extends ICompletionPlugin {
wrappedFlow = isFlow(x.value.value, pm)
}
})
Some(RefYamlKind.map(fl, wrappedInMap, wrappedFlow, off))
Some(RefYamlKind.map(fl || openBracket, wrappedInMap, wrappedFlow, off))
case _ => None
}
})
})
})
}

def toTemplateSuggestion(decl: Declaration, kind: RefYamlKind): Option[TemplateSuggestion] = {
def toTemplateSuggestion(decl: Declaration, kind: RefYamlKind, prefix: String): Option[TemplateSuggestion] = {
val declNode = decl.node
val nameOpt = declNode.attribute("name").flatMap(_.value).map(_.toString)
if (nameOpt.isEmpty) {
Expand All @@ -211,12 +229,10 @@ class TemplateReferencesCompletionPlugin extends ICompletionPlugin {
val params = declNode.attributes("parameters").flatMap(_.value).map(_.toString)
if (isTrait) {
if (params.isEmpty) {
if (kind.inSequence) {
Some(TemplateSuggestion(name, name, kind))
} else if (kind.inMap && !kind.flow) {
if (kind.inMap && !kind.flow) {
Some(TemplateSuggestion(name, s"- $name", kind))
} else {
Some(TemplateSuggestion(name, s"[ $name ]", kind))
Some(TemplateSuggestion(name, name, kind))
}
} else {
if (kind.inMap && !kind.flow) {
Expand Down Expand Up @@ -307,11 +323,11 @@ class TemplateReferencesCompletionPlugin extends ICompletionPlugin {
}

object TemplateReferencesCompletionPlugin {
val ID = "templateRef.completion";
val ID = "templateRef.completion"

val supportedLanguages: List[Vendor] = List(Raml10);
val supportedLanguages: List[Vendor] = List(Raml10)

def apply(): TemplateReferencesCompletionPlugin = new TemplateReferencesCompletionPlugin();
def apply(): TemplateReferencesCompletionPlugin = new TemplateReferencesCompletionPlugin()
}

case class RefYamlKind(inSequence: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#%RAML 1.0
title: test API

traits:
trait1:
queryParameters:
qp1:
trait2:
queryParameters:
qp2:

/resource:
get:
is: tr*
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#%RAML 1.0
title: test API

traits:
trait1:
queryParameters:
qp1:
trait2:
queryParameters:
qp2:

/resource:
get:
is: [ trait1, tr* ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#%RAML 1.0
title: test API

traits:
trait1:
queryParameters:
qp1:
trait2:
queryParameters:
qp2:

/resource:
get:
is: [ tr*
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#%RAML 1.0
title: test API

traits:
trait1:
queryParameters:
qp1:
trait2:
queryParameters:
qp2:

/resource:
get:
is:
- trait1
- tr*
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#%RAML 1.0
title: test API

traits:
trait1:
queryParameters:
qp1:
trait2:
queryParameters:
qp2:

/resource:
get:
is: [ trait1, tr*
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,25 @@ class JiraSuggestionsTests extends RAML10Test {
test("ALS-722 check prefix in included directory") {
this.runTest("jira-tests/als-722/api.raml", Set("dataType.raml"))
}

test("ALS-732 Single") {
this.runTest("jira-tests/als-732/test01.raml", Set("trait1", "trait2"))
}

test("ALS-732 Sequence") {
this.runTest("jira-tests/als-732/test02.raml", Set("trait2"))
}

test("ALS-732 Open Sequence") {
this.runTest("jira-tests/als-732/test03.raml", Set("trait1", "trait2"))
}

test("ALS-732 Flow Sequence") {
this.runTest("jira-tests/als-732/test04.raml", Set("trait2"))
}

//TODO: Check written values in non valid array
ignore("ALS-732 Open Sequence with trait") {
this.runTest("jira-tests/als-732/test04.raml", Set("trait2"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.mulesoft.als.suggestions.test.raml10
class TraitReferenceTests extends RAML10Test {

test("test001") {
this.runTest("traitReferences/test001.raml", Set("[ trait1 ]", "[ trait2 ]"))
this.runTest("traitReferences/test001.raml", Set("trait1", "trait2"))
}

test("test002") {
Expand Down

0 comments on commit 06aef68

Please sign in to comment.