Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import org.mulesoft.als.common.DirectoryResolver
import org.mulesoft.als.common.cache.{ASTPartBranchCached, ObjectInTreeCached}
import org.mulesoft.als.common.dtoTypes.PositionRange
import org.mulesoft.als.configuration.AlsConfigurationReader
import org.mulesoft.amfintegration.amfconfiguration.ALSConfigurationState
import org.mulesoft.amfintegration.amfconfiguration.{ALSConfigurationState, DocumentDefinition}
import org.mulesoft.amfintegration.relationships.RelationshipLink
import org.mulesoft.lsp.feature.codeactions.CodeActionParams

case class CodeActionRequestParams(
uri: String,
range: PositionRange,
bu: BaseUnit,
tree: ObjectInTreeCached,
astPartBranchCached: ASTPartBranchCached,
definedBy: Dialect,
configuration: AlsConfigurationReader,
allRelationships: Seq[RelationshipLink],
alsConfigurationState: ALSConfigurationState,
uuid: String,
directoryResolver: DirectoryResolver
uri: String,
range: PositionRange,
bu: BaseUnit,
tree: ObjectInTreeCached,
astPartBranchCached: ASTPartBranchCached,
documentDefinition: DocumentDefinition,
configuration: AlsConfigurationReader,
allRelationships: Seq[RelationshipLink],
alsConfigurationState: ALSConfigurationState,
uuid: String,
directoryResolver: DirectoryResolver
) {

val findDialectForSemantic: String => Option[(SemanticExtension, Dialect)] =
Expand All @@ -32,23 +32,23 @@ case class CodeActionRequestParams(
object CodeActionParamsImpl {
implicit class CodeActionParamsImpl(param: CodeActionParams) {
def toRequestParams(
bu: BaseUnit,
tree: ObjectInTreeCached,
astPartBranch: ASTPartBranchCached,
dialect: Dialect,
configuration: AlsConfigurationReader,
allRelationships: Seq[RelationshipLink],
alsConfigurationState: ALSConfigurationState,
uuid: String,
directoryResolver: DirectoryResolver
bu: BaseUnit,
tree: ObjectInTreeCached,
astPartBranch: ASTPartBranchCached,
documentDefinition: DocumentDefinition,
configuration: AlsConfigurationReader,
allRelationships: Seq[RelationshipLink],
alsConfigurationState: ALSConfigurationState,
uuid: String,
directoryResolver: DirectoryResolver
): CodeActionRequestParams =
CodeActionRequestParams(
param.textDocument.uri,
PositionRange(param.range),
bu,
tree,
astPartBranch,
dialect,
documentDefinition,
configuration,
allRelationships,
alsConfigurationState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class JsonSchemaToRamlType(override protected val params: CodeActionRequestParam

private def renderRamlType(shape: AnyShape): Future[String] = Future {
shape.annotations.reject(_.isInstanceOf[ParsedJSONSchema])
val node: Option[YNode] = declaredElementNode(Some(shape), params.definedBy, alsConfigurationState)
val node: Option[YNode] = declaredElementNode(Some(shape), params.documentDefinition, alsConfigurationState)
val parent = yPartBranch.flatMap(_.closestEntry)
node
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import amf.core.client.scala.model.domain.AmfObject
import org.mulesoft.als.actions.codeactions.plugins.declarations.common.BaseElementDeclarableExtractors
import org.mulesoft.als.common.ObjectInTree
import org.mulesoft.amfintegration.AmfImplicits.AmfAnnotationsImp
import org.mulesoft.amfintegration.amfconfiguration.DocumentDefinition

trait ExtractRamlType extends BaseElementDeclarableExtractors {

protected override lazy val amfObject: Option[AmfObject] = extractRamlType

protected def extractRamlType: Option[AmfObject] =
extractJsonSchema(maybeTree, params.definedBy) orElse extractAmfObject(maybeTree, params.definedBy)
extractJsonSchema(maybeTree, params.documentDefinition) orElse extractAmfObject(maybeTree, params.documentDefinition)

/** Get the RAML type definition that encapsulates the Json schema, not the Json schema shape
*/
protected override def extractable(maybeObject: Option[AmfObject], dialect: Dialect): Option[AmfObject] =
super.extractable(maybeObject, dialect).filterNot(_.annotations.schemeIsJsonSchema)
protected override def extractable(maybeObject: Option[AmfObject], documentDefinition: DocumentDefinition): Option[AmfObject] =
super.extractable(maybeObject, documentDefinition).filterNot(_.annotations.schemeIsJsonSchema)

/** Extract Json schema as a whole, do not allow to extract individual properties
*/
private def extractJsonSchema(maybeTree: Option[ObjectInTree], dialect: Dialect): Option[AmfObject] =
extractable(maybeTree.flatMap(_.stack.dropWhile(!_.annotations.schemeIsJsonSchema).drop(1).headOption), dialect)
private def extractJsonSchema(maybeTree: Option[ObjectInTree], documentDefinition: DocumentDefinition): Option[AmfObject] =
extractable(maybeTree.flatMap(_.stack.dropWhile(!_.annotations.schemeIsJsonSchema).drop(1).headOption), documentDefinition)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.mulesoft.als.common.dtoTypes.PositionRange
import org.mulesoft.als.convert.LspRangeConverter
import org.mulesoft.als.declarations.DeclarationCreator
import org.mulesoft.amfintegration.AmfImplicits.{AmfAnnotationsImp, AmfObjectImp, BaseUnitImp, DialectImplicits}
import org.mulesoft.amfintegration.amfconfiguration.DocumentDefinition
import org.mulesoft.lsp.edit.TextEdit
import org.mulesoft.lsp.feature.common.Range
import org.yaml.model._
Expand All @@ -29,26 +30,26 @@ trait BaseElementDeclarableExtractors extends TreeKnowledge with DeclarationCrea

private lazy val baseName: String =
amfObject
.flatMap(ExtractorCommon.declarationName(_, params.definedBy))
.flatMap(ExtractorCommon.declarationName(_, params.documentDefinition))
.getOrElse("newDeclaration")

/** Placeholder for the new name (key and reference)
*/
protected def newName: String = ExtractorCommon.nameNotInList(baseName, params.bu.declaredNames.toSet)

protected lazy val amfObject: Option[AmfObject] =
extractAmfObject(maybeTree, params.definedBy)
extractAmfObject(maybeTree, params.documentDefinition)

/** Selected object if there is a clean match in the range and it is a declarable, or the parents range
*/
protected final def extractAmfObject(maybeTree: Option[ObjectInTree], dialect: Dialect): Option[AmfObject] =
extractable(maybeTree.map(_.obj), dialect) orElse
extractable(maybeTree.flatMap(_.stack.headOption), dialect)
protected final def extractAmfObject(maybeTree: Option[ObjectInTree], documentDefinition: DocumentDefinition): Option[AmfObject] =
extractable(maybeTree.map(_.obj), documentDefinition) orElse
extractable(maybeTree.flatMap(_.stack.headOption), documentDefinition)

protected def extractable(maybeObject: Option[AmfObject], dialect: Dialect): Option[AmfObject] =
protected def extractable(maybeObject: Option[AmfObject], documentDefinition: DocumentDefinition): Option[AmfObject] =
maybeObject
.filterNot(_.isInstanceOf[Document])
.find(o => o.declarableKey(dialect).isDefined)
.find(o => o.declarableKey(documentDefinition).isDefined)

/** The original node with lexical info for the declared node
*/
Expand Down Expand Up @@ -110,7 +111,7 @@ trait BaseElementDeclarableExtractors extends TreeKnowledge with DeclarationCrea
JsonRender.render(rl.getOrElse(jsonRefEntry), entryIndentation + jsonOptions.indentationSize, jsonOptions)
)
)
else if (params.definedBy.isJsonStyle)
else if (params.documentDefinition.isJsonStyle)
entryRange.map(
TextEdit(
_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait ConverterExtractor[Original <: AmfObject, Result <: AmfObject]
newName,
params.bu,
params.uri,
params.definedBy,
params.documentDefinition,
params.configuration,
jsonOptions,
yamlOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.mulesoft.als.actions.codeactions.plugins.declarations.common

import amf.aml.client.scala.model.document.Dialect
import amf.core.client.scala.model.document.BaseUnit
import amf.core.client.scala.model.domain.AmfObject
import org.mulesoft.als.actions.codeactions.plugins.declarations.common.ExtractorCommon.{
Expand All @@ -9,7 +8,7 @@ import org.mulesoft.als.actions.codeactions.plugins.declarations.common.Extracto
}
import org.mulesoft.als.common.YPartASTWrapper.YNodeImplicits
import org.mulesoft.als.configuration.AlsConfigurationReader
import org.mulesoft.amfintegration.amfconfiguration.ALSConfigurationState
import org.mulesoft.amfintegration.amfconfiguration.{ALSConfigurationState, DocumentDefinition}
import org.yaml.model.YMapEntry
import org.yaml.render.{JsonRenderOptions, YamlRenderOptions}

Expand All @@ -18,18 +17,18 @@ trait DeclarationWrapper {
val declarationKey: String

def wrapDeclaration(
amfObject: Option[AmfObject],
newName: String,
bu: BaseUnit,
uri: String,
dialect: Dialect,
configurationReader: AlsConfigurationReader,
jsonOptions: JsonRenderOptions,
yamlOptions: YamlRenderOptions,
alsConfigurationState: ALSConfigurationState
amfObject: Option[AmfObject],
newName: String,
bu: BaseUnit,
uri: String,
documentDefinition: DocumentDefinition,
configurationReader: AlsConfigurationReader,
jsonOptions: JsonRenderOptions,
yamlOptions: YamlRenderOptions,
alsConfigurationState: ALSConfigurationState
): Option[(String, Option[YMapEntry])] = {
val existingDeclaration = ExtractorCommon.findExistingKeyPart(bu, uri, Seq(declarationKey))
declaredElementNode(amfObject, dialect, alsConfigurationState)
declaredElementNode(amfObject, documentDefinition, alsConfigurationState)
.map(node => {
val r = node.withKey(newName)
if (existingDeclaration.isEmpty) r.withKey(declarationKey) else r
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.mulesoft.als.common.dtoTypes.PositionRange
import org.mulesoft.als.configuration.AlsConfigurationReader
import org.mulesoft.als.declarations.DeclarationCreator
import org.mulesoft.amfintegration.AmfImplicits.{AmfAnnotationsImp, AmfObjectImp, BaseUnitImp}
import org.mulesoft.amfintegration.amfconfiguration.ALSConfigurationState
import org.mulesoft.amfintegration.amfconfiguration.{ALSConfigurationState, DocumentDefinition}
import org.mulesoft.common.collections._
import org.yaml.model._
import org.yaml.render.{JsonRender, JsonRenderOptions, YamlRender, YamlRenderOptions}
Expand All @@ -22,17 +22,17 @@ object ExtractorCommon extends DeclarationCreator {
!bu.declarations.forall(objs.contains)

def existAnyDeclaration(
objs: Seq[AmfObject],
astPartBranch: Option[ASTPartBranch],
bu: BaseUnit,
dialect: Dialect
objs: Seq[AmfObject],
astPartBranch: Option[ASTPartBranch],
bu: BaseUnit,
documentDefinition: DocumentDefinition
): Seq[PositionRange] =
if (!existAnyOtherDeclaration(objs, bu))
deleteAll(objs, astPartBranch, bu, dialect)
else deleteDeclarationGroup(objs, bu, dialect)
deleteAll(objs, astPartBranch, bu, documentDefinition)
else deleteDeclarationGroup(objs, bu, documentDefinition)

private def deleteAll(objs: Seq[AmfObject], astPartBranch: Option[ASTPartBranch], bu: BaseUnit, dialect: Dialect) =
declarationPathForDialect(dialect) match {
private def deleteAll(objs: Seq[AmfObject], astPartBranch: Option[ASTPartBranch], bu: BaseUnit, documentDefinition: DocumentDefinition) =
declarationPathForDialect(documentDefinition) match {
case Some(d) =>
val flatten: Option[YMapEntry] = astPartBranch
.map(_.stack)
Expand All @@ -41,11 +41,11 @@ object ExtractorCommon extends DeclarationCreator {
.collectFirst({ case m: YMap => m.entries.find(_.key.asScalar.exists(_.text == d)) })
.flatten
flatten.map(_.range).map(PositionRange(_)).toSeq
case _ => deleteDeclarationGroup(objs, bu, dialect)
case _ => deleteDeclarationGroup(objs, bu, documentDefinition)
}

private def deleteDeclarationGroup(objs: Seq[AmfObject], bu: BaseUnit, dialect: Dialect): Seq[PositionRange] = {
val objsByKey: Map[Option[String], Seq[AmfObject]] = objs.legacyGroupBy(_.declarableKey(dialect))
private def deleteDeclarationGroup(objs: Seq[AmfObject], bu: BaseUnit, documentDefinition: DocumentDefinition): Seq[PositionRange] = {
val objsByKey: Map[Option[String], Seq[AmfObject]] = objs.legacyGroupBy(_.declarableKey(documentDefinition))
val allRanges =
if (!objsByKey.keySet.contains(None))
objsByKey.iterator.flatMap { t =>
Expand All @@ -69,20 +69,20 @@ object ExtractorCommon extends DeclarationCreator {
* Element as YNode
*/
def declaredElementNode(
amfObject: Option[AmfObject],
dialect: Dialect,
alsConfigurationState: ALSConfigurationState
amfObject: Option[AmfObject],
documentDefinition: DocumentDefinition,
alsConfigurationState: ALSConfigurationState
): Option[YNode] =
amfObject
.collect { case e: DomainElement =>
alsConfigurationState.configForDialect(dialect).emit(e)
alsConfigurationState.configForDefinition(documentDefinition).emit(e)
}

/** The complete node and the entry where it belongs, contemplating the path for the declaration and existing AST
*/
def wrappedDeclaredEntry(
amfObject: Option[AmfObject],
dialect: Dialect,
dialect: DocumentDefinition,
bu: BaseUnit,
uri: String,
newName: String,
Expand All @@ -103,17 +103,17 @@ object ExtractorCommon extends DeclarationCreator {
/** Render for the new declaration, and the top entry on which it should be nested
*/
def declaredEntry(
amfObject: Option[AmfObject],
dialect: Dialect,
bu: BaseUnit,
uri: String,
newName: String,
configurationReader: AlsConfigurationReader,
jsonOptions: JsonRenderOptions,
yamlOptions: YamlRenderOptions,
alsConfigurationState: ALSConfigurationState
amfObject: Option[AmfObject],
documentDefinition: DocumentDefinition,
bu: BaseUnit,
uri: String,
newName: String,
configurationReader: AlsConfigurationReader,
jsonOptions: JsonRenderOptions,
yamlOptions: YamlRenderOptions,
alsConfigurationState: ALSConfigurationState
): Option[(String, Option[YMapEntry])] = {
val wrapped = wrappedDeclaredEntry(amfObject, dialect, bu, uri, newName, alsConfigurationState)
val wrapped = wrappedDeclaredEntry(amfObject, documentDefinition, bu, uri, newName, alsConfigurationState)
val maybeParent: Option[YMapEntry] = wrapped.flatMap(_._2)
wrapped
.map(_._1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DeleteDeclaredNodeCodeAction(override val params: CodeActionRequestParams)

private def removeObj(obj: AmfObject) =
ExtractorCommon
.existAnyDeclaration(Seq(obj), yPartBranch, params.bu, params.definedBy)
.existAnyDeclaration(Seq(obj), yPartBranch, params.bu, params.documentDefinition)
.map(toLspRange)
.map(r => TextEdit(r.copy(start = r.start.copy(character = 0)), ""))
.headOption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ case class ExtractRamlDeclarationToFragmentCodeAction(params: CodeActionRequestP
override protected val kindTitle: CodeActionKindTitle = ExtractRamlTypeToFragmentCodeAction

override lazy val amfObject: Option[AmfObject] = {
val maybeObject = extractAmfObject(maybeTree, params.definedBy)
val maybeObject = extractAmfObject(maybeTree, params.documentDefinition)
fragmentBundleForObject(maybeObject).fold { // if empty
maybeTree.flatMap(t => t.stack.headOption).collect { case d: CustomDomainProperty =>
d // declared annotation type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ case class ExtractRamlTypeToFragmentCodeAction(params: CodeActionRequestParams)
spec.isRaml && positionIsExtracted &&
amfObject.exists(o =>
ExtractorCommon
.declarationPath(o, params.alsConfigurationState.definitionFor(spec).getOrElse(params.definedBy)) == Seq(
.declarationPath(o, params.alsConfigurationState.definitionFor(spec).getOrElse(params.documentDefinition)) == Seq(
"types"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ trait ExtractDeclarationsToLibrary extends CodeActionResponsePlugin with Creates
.map(_.start)
.map(p => params.astPartBranchCached.getCachedOrNew(p, params.uri)),
params.bu,
params.definedBy
params.documentDefinition
)
// as it is just for RAML (ergo YAML), we will take the whole line as the entry to be erased
.map(range => Range(Position(range.start.line, 0), LspRangeConverter.toLspPosition(range.`end`)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ case class ExtractElementCodeAction(params: CodeActionRequestParams) extends Ext

override lazy val isApplicable: Boolean =
homogeneousVendor && !spec.isRaml && amfObject.isDefined && positionIsExtracted &&
params.definedBy != MetaDialect.dialect && appliesToDocument()
params.documentDefinition.baseUnit != MetaDialect.dialect && appliesToDocument()

override protected def msg(params: CodeActionRequestParams): String =
s"Extract element to declaration: \n\t${params.uri}\t${params.range}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ case class ExtractRamlTypeCodeAction(params: CodeActionRequestParams)

override lazy val isApplicable: Boolean =
homogeneousVendor && spec.isRaml && positionIsExtracted &&
amfObject.exists(o => ExtractorCommon.declarationPath(o, params.definedBy) == Seq("types")) && appliesToDocument()
amfObject.exists(o => ExtractorCommon.declarationPath(o, params.documentDefinition) == Seq("types")) && appliesToDocument()

override lazy val linkEntry: Future[Option[TextEdit]] =
renderLink.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ trait ExtractSameFileDeclaration extends CodeActionResponsePlugin with ShapeExtr

protected def appliesToDocument(): Boolean =
!params.bu.isFragment || params.bu
.documentMapping(params.definedBy)
.documentMapping(params.documentDefinition)
.exists(_.declaredNodes().exists(dn => amfObject.exists(_.id == dn.mappedNode().value())))

protected lazy val declaredElementTextEdit: Option[TextEdit] =
Expand All @@ -52,7 +52,7 @@ trait ExtractSameFileDeclaration extends CodeActionResponsePlugin with ShapeExtr
ExtractorCommon
.declaredEntry(
amfObject,
params.definedBy,
params.documentDefinition,
params.bu,
params.uri,
name,
Expand Down Expand Up @@ -85,7 +85,7 @@ trait ExtractSameFileDeclaration extends CodeActionResponsePlugin with ShapeExtr
l.annotations += DeclaredElement()
val linkDe: DomainElement = l.link(newName)
linkDe.annotations += ForceEntry() // raml explicit types
params.alsConfigurationState.configForDialect(params.definedBy).emit(linkDe)
params.alsConfigurationState.configForDefinition(params.documentDefinition).emit(linkDe)
}
}

Expand Down
Loading