diff --git a/als-suggestions/shared/src/main/scala/org/mulesoft/als/suggestions/CompletionProviderAST.scala b/als-suggestions/shared/src/main/scala/org/mulesoft/als/suggestions/CompletionProviderAST.scala index 08e87b9ca..9019ac4ae 100644 --- a/als-suggestions/shared/src/main/scala/org/mulesoft/als/suggestions/CompletionProviderAST.scala +++ b/als-suggestions/shared/src/main/scala/org/mulesoft/als/suggestions/CompletionProviderAST.scala @@ -1,9 +1,11 @@ package org.mulesoft.als.suggestions -import org.mulesoft.als.common.YPartBranch +import org.mulesoft.als.common.{ASTPartBranch, YPartBranch, YamlUtils} import org.mulesoft.als.suggestions.aml.AmlCompletionRequest import org.mulesoft.als.suggestions.interfaces._ +import org.mulesoft.common.client.lexical.Position import org.mulesoft.lsp.feature.completion.CompletionItem +import org.yaml.model.YComment import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future @@ -22,6 +24,7 @@ class CompletionProviderAST(request: AmlCompletionRequest) extends CompletionPro override def suggest(): Future[Seq[CompletionItem]] = if (request.astPartBranch.isMultiline) Future.successful(Nil) + else if (isInsideComment(request.position.toAmfPosition, request.astPartBranch)) Future.successful(Nil) else request.completionsPluginHandler .pluginSuggestions(request) @@ -31,6 +34,20 @@ class CompletionProviderAST(request: AmlCompletionRequest) extends CompletionPro .filterNot(rs => arraySiblings(rs.newText)) .map(request.styler.rawToStyledSuggestion) }) + + private def isInsideComment(position: Position, astPartBranch: ASTPartBranch): Boolean = + astPartBranch match { + case yPart: YPartBranch => + // we are inside a comment if the current node is an empty YScalar and the request position matches a YComment on the parent node + yPart.isEmptyNode && yPart.parentEntry.exists(parent => { + parent.children.exists { + case comment: YComment if YamlUtils.contains(comment.range, position) => true + case _ => false + } + + }) + case _ => false + } } object CompletionProviderAST { diff --git a/als-suggestions/shared/src/test/resources/test/raml10/by-directory/types/expected/type-with-comment.raml.json b/als-suggestions/shared/src/test/resources/test/raml10/by-directory/types/expected/type-with-comment.raml.json index e69c0a62d..c9c513b9b 100644 --- a/als-suggestions/shared/src/test/resources/test/raml10/by-directory/types/expected/type-with-comment.raml.json +++ b/als-suggestions/shared/src/test/resources/test/raml10/by-directory/types/expected/type-with-comment.raml.json @@ -1,470 +1,3 @@ [ - { - "label": "any", - "kind": [ - - ], - "detail": "unknown", - "documentation": "any", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11any", - "filterText": "any", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "any" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "array", - "kind": [ - - ], - "detail": "unknown", - "documentation": "array", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11array", - "filterText": "array", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "array" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "boolean", - "kind": [ - - ], - "detail": "unknown", - "documentation": "boolean", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11boolean", - "filterText": "boolean", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "boolean" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "date-only", - "kind": [ - - ], - "detail": "unknown", - "documentation": "date-only", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11date-only", - "filterText": "date-only", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "date-only" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "datetime", - "kind": [ - - ], - "detail": "unknown", - "documentation": "datetime", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11datetime", - "filterText": "datetime", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "datetime" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "datetime-only", - "kind": [ - - ], - "detail": "unknown", - "documentation": "datetime-only", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11datetime-only", - "filterText": "datetime-only", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "datetime-only" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "file", - "kind": [ - - ], - "detail": "unknown", - "documentation": "file", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11file", - "filterText": "file", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "file" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "integer", - "kind": [ - - ], - "detail": "unknown", - "documentation": "integer", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11integer", - "filterText": "integer", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "integer" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "nil", - "kind": [ - - ], - "detail": "unknown", - "documentation": "nil", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11nil", - "filterText": "nil", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "nil" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "number", - "kind": [ - - ], - "detail": "unknown", - "documentation": "number", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11number", - "filterText": "number", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "number" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "object", - "kind": [ - - ], - "detail": "unknown", - "documentation": "object", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11object", - "filterText": "object", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "object" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "string", - "kind": [ - - ], - "detail": "unknown", - "documentation": "string", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11string", - "filterText": "string", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "string" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - }, - { - "label": "time-only", - "kind": [ - - ], - "detail": "unknown", - "documentation": "time-only", - "deprecated": [ - - ], - "preselect": [ - - ], - "sortText": "11time-only", - "filterText": "time-only", - "insertText": null, - "insertTextFormat": [ - 1 - ], - "textEdit": { - "range": { - "start": { - "line": 5, - "character": 14 - }, - "end": { - "line": 5, - "character": 14 - } - }, - "newText": "time-only" - }, - "additionalTextEdits": null, - "commitCharacters": null, - "command": null - } + ] \ No newline at end of file diff --git a/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-before-comment.yaml.json b/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-before-comment.yaml.json new file mode 100644 index 000000000..108d398fd --- /dev/null +++ b/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-before-comment.yaml.json @@ -0,0 +1,38 @@ +[ + { + "label": "string", + "kind": [ + + ], + "detail": "unknown", + "documentation": "string", + "deprecated": [ + + ], + "preselect": [ + + ], + "sortText": "10string", + "filterText": "string", + "insertText": null, + "insertTextFormat": [ + 1 + ], + "textEdit": { + "range": { + "start": { + "line": 3, + "character": 10 + }, + "end": { + "line": 3, + "character": 11 + } + }, + "newText": "string" + }, + "additionalTextEdits": null, + "commitCharacters": null, + "command": null + } +] diff --git a/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment-key.yaml.json b/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment-key.yaml.json new file mode 100644 index 000000000..c9c513b9b --- /dev/null +++ b/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment-key.yaml.json @@ -0,0 +1,3 @@ +[ + +] \ No newline at end of file diff --git a/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment-scalar.yaml.json b/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment-scalar.yaml.json new file mode 100644 index 000000000..c9c513b9b --- /dev/null +++ b/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment-scalar.yaml.json @@ -0,0 +1,3 @@ +[ + +] \ No newline at end of file diff --git a/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment.yaml.json b/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment.yaml.json new file mode 100644 index 000000000..c9c513b9b --- /dev/null +++ b/als-suggestions/shared/src/test/resources/test/yaml/expected/yaml-inside-comment.yaml.json @@ -0,0 +1,3 @@ +[ + +] \ No newline at end of file diff --git a/als-suggestions/shared/src/test/resources/test/yaml/yaml-before-comment.yaml b/als-suggestions/shared/src/test/resources/test/yaml/yaml-before-comment.yaml new file mode 100644 index 000000000..dcd577436 --- /dev/null +++ b/als-suggestions/shared/src/test/resources/test/yaml/yaml-before-comment.yaml @@ -0,0 +1,4 @@ +swagger: '2.0' +definitions: + a: + type: s*# diff --git a/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment-key.yaml b/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment-key.yaml new file mode 100644 index 000000000..2f90e12c8 --- /dev/null +++ b/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment-key.yaml @@ -0,0 +1,6 @@ +swagger: '2.0' +definitions: + a: + type: string + name: + #* \ No newline at end of file diff --git a/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment-scalar.yaml b/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment-scalar.yaml new file mode 100644 index 000000000..8938797b2 --- /dev/null +++ b/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment-scalar.yaml @@ -0,0 +1,4 @@ +swagger: '2.0' +definitions: + a: + type: #s* \ No newline at end of file diff --git a/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment.yaml b/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment.yaml new file mode 100644 index 000000000..be70bc4f7 --- /dev/null +++ b/als-suggestions/shared/src/test/resources/test/yaml/yaml-inside-comment.yaml @@ -0,0 +1,6 @@ +swagger: '2.0' +definitions: + a: + type: string + name: + $ref: #* \ No newline at end of file