Skip to content

Commit

Permalink
Merge pull request #21 from inxilpro/autocomplete-refactor
Browse files Browse the repository at this point in the history
Autocomplete refactor + Alpine V3 support
  • Loading branch information
inxilpro authored Jun 14, 2021
2 parents 8417487 + cc4cafe commit d633969
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 160 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

- Added support for PHP and Blade fragments inside of Alpine directives
- Added Alpine gutter icon for easier identification of lines that have Alpine directives
- Added support for Alpine v3 directives and magics
- Added better support for modifiers like `@click.prevent`

### Changed

- Updated auto-complete to only trigger after `x-bind:` or `x-on:`
- Improved auto-complete logic

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
## Plugin Description

<!-- Plugin description -->
![intellij-alpine](https://user-images.githubusercontent.com/21592/97035246-0b7a9e00-1534-11eb-9722-2492f380eca3.gif)
![intellij-alpine](https://user-images.githubusercontent.com/21592/121929093-d7e0c400-cd0e-11eb-8ff3-d52db5831a55.gif)

This plugin adds support for the following [Alpine.js](https://github.com/alpinejs/alpine) features:

Expand Down
2 changes: 2 additions & 0 deletions detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ formatting:
active: false
NoTrailingSpaces:
active: false
StringTemplate:
active: false

complexity:
NestedBlockDepth:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.completion.CompletionUtilCore
import com.intellij.codeInsight.completion.HtmlCompletionContributor
import com.intellij.codeInsight.completion.XmlAttributeInsertHandler
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.lang.html.HTMLLanguage
Expand All @@ -13,8 +12,7 @@ import com.intellij.psi.html.HtmlTag
import com.intellij.psi.xml.XmlAttribute
import com.intellij.util.ProcessingContext

class AlpineAttributeCompletionProvider(vararg items: String) :
CompletionProvider<CompletionParameters?>() {
class AlpineAttributeCompletionProvider(vararg items: String) : CompletionProvider<CompletionParameters?>() {

@Suppress("ReturnCount")
public override fun addCompletions(
Expand All @@ -23,9 +21,6 @@ class AlpineAttributeCompletionProvider(vararg items: String) :
result: CompletionResultSet
) {
val position = parameters.position
if (!HtmlCompletionContributor.hasHtmlAttributesCompletion(position)) {
return
}

if (HTMLLanguage.INSTANCE !in position.containingFile.viewProvider.languages) {
return
Expand All @@ -34,49 +29,34 @@ class AlpineAttributeCompletionProvider(vararg items: String) :
val attribute = position.parent as? XmlAttribute ?: return
val xmlTag = attribute.parent as? HtmlTag ?: return

// CompletionUtilCore.DUMMY_IDENTIFIER
val attributeName = StringUtil.trimEnd(attribute.name, CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED)
val partialAttribute = StringUtil.trimEnd(attribute.name, CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED)

for (prefix in AttributeUtil.xmlPrefixes) {
if (prefix.startsWith(attributeName) && attributeName.length < prefix.length) {
val info = AttributeInfo(prefix)
val elementBuilder = LookupElementBuilder
.create(prefix)
.withCaseSensitivity(false)
.withIcon(Alpine.ICON)
.withTypeText(info.typeText)

result.addElement(elementBuilder)
}
if (partialAttribute.isEmpty()) {
return
}

for (info in AttributeUtil.getValidAttributesWithInfo(xmlTag)) {
if (!prefixMatchesAttribute(attributeName, info.attribute)) {
continue
val suggestions = AutoCompleteSuggestions(xmlTag, partialAttribute)

suggestions.descriptors.forEach {
var text = it.attribute

// If you go back and add a modifier, it ignores the prefix, so we'll
// just kinda code around that for now
if (text.contains(':') && text.contains('.')) {
text = text.substringAfter(':')
}

var elementBuilder = LookupElementBuilder
.create(info.attribute)
.create(text)
.withCaseSensitivity(false)
.withIcon(Alpine.ICON)
.withTypeText(info.typeText)
.withTypeText(it.typeText)

if (info.hasValue()) {
if (it.hasValue() && !it.canBePrefix()) {
elementBuilder = elementBuilder.withInsertHandler(XmlAttributeInsertHandler.INSTANCE)
}

result.addElement(elementBuilder)
}
}

private fun prefixMatchesAttribute(prefix: String, attribute: String): Boolean {
if (!attribute.contains(':')) {
return true
}

if (!prefix.contains(':')) {
return false
}

return attribute.startsWith(prefix, true)
}
}
Loading

0 comments on commit d633969

Please sign in to comment.