-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added completion and doc provider for printf/scanf format specifiers (#…
…23)
- Loading branch information
Showing
7 changed files
with
406 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/net/king2500/plugins/PhpAdvancedAutoComplete/PhpAutoPopupTypedHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.king2500.plugins.PhpAdvancedAutoComplete; | ||
|
||
import com.intellij.codeInsight.editorActions.TypedHandlerDelegate; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.ObjectUtils; | ||
import com.jetbrains.php.completion.PhpCompletionUtil; | ||
import com.jetbrains.php.lang.psi.PhpFile; | ||
import com.jetbrains.php.lang.psi.PhpPsiUtil; | ||
import com.jetbrains.php.lang.psi.elements.FunctionReference; | ||
import com.jetbrains.php.lang.psi.elements.ParameterList; | ||
import com.jetbrains.php.lang.psi.elements.Statement; | ||
import net.king2500.plugins.PhpAdvancedAutoComplete.utils.PhpElementsUtil; | ||
import net.king2500.plugins.PhpAdvancedAutoComplete.utils.StringUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* @author Thomas Schulz <[email protected]> | ||
*/ | ||
public class PhpAutoPopupTypedHandler extends TypedHandlerDelegate { | ||
@NotNull | ||
@Override | ||
public Result checkAutoPopup(char charTyped, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { | ||
|
||
if (!(file instanceof PhpFile)) { | ||
return Result.CONTINUE; | ||
} | ||
|
||
int offset = editor.getCaretModel().getOffset(); | ||
PsiElement psiElement = file.findElementAt(offset); | ||
|
||
ParameterList parameterList = PhpPsiUtil.getParentByCondition(psiElement, true, ParameterList.INSTANCEOF, Statement.INSTANCEOF); | ||
if (parameterList != null) { | ||
FunctionReference functionCall = ObjectUtils.tryCast(parameterList.getParent(), FunctionReference.class); | ||
String fqn = PhpElementsUtil.resolveFqn(functionCall); | ||
|
||
if (PhpElementsUtil.isFormatFunction(fqn) && charTyped == '%') { | ||
if (StringUtil.getPrecedingCharNum(editor.getDocument().getCharsSequence(), offset, '%') % 2 == 0) { | ||
PhpCompletionUtil.showCompletion(editor); | ||
} | ||
} | ||
} | ||
|
||
return Result.CONTINUE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/net/king2500/plugins/PhpAdvancedAutoComplete/PhpFormatDocumentationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package net.king2500.plugins.PhpAdvancedAutoComplete; | ||
|
||
import com.intellij.lang.Language; | ||
import com.intellij.lang.documentation.DocumentationProviderEx; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiManager; | ||
import com.intellij.psi.impl.light.LightElement; | ||
import com.jetbrains.php.lang.psi.PhpPsiUtil; | ||
import com.jetbrains.php.lang.psi.elements.FunctionReference; | ||
import com.jetbrains.php.lang.psi.elements.Statement; | ||
import io.netty.util.internal.StringUtil; | ||
import net.king2500.plugins.PhpAdvancedAutoComplete.utils.PhpElementsUtil; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Thomas Schulz <[email protected]> | ||
*/ | ||
public class PhpFormatDocumentationProvider extends DocumentationProviderEx { | ||
|
||
private static final String PHP_FORMAT_URL = "https://www.php.net/manual/en/function.{functionName}.php"; | ||
|
||
@Override | ||
public @Nullable List<String> getUrlFor(PsiElement element, PsiElement originalElement) { | ||
if (!(element instanceof FormatTokenDocElement)) { | ||
return null; | ||
} | ||
String functionName = ((FormatTokenDocElement)element).getFunctionName(); | ||
return Collections.singletonList(PHP_FORMAT_URL.replace("{functionName}", functionName)); | ||
} | ||
|
||
@Override | ||
public @Nullable String generateDoc(PsiElement element, @Nullable PsiElement originalElement) { | ||
if (!(element instanceof FormatTokenDocElement)) { | ||
return null; | ||
} | ||
|
||
String tokenText = ((FormatTokenDocElement)element).getTokenText(); | ||
String functionName = ((FormatTokenDocElement)element).getFunctionName(); | ||
|
||
if (Arrays.asList(PhpCompletionTokens.scanFormatFuncs).contains(functionName + ":1")) { | ||
return PhpCompletionTokens.scanFormatTokensDoc.getOrDefault(tokenText, ""); | ||
} | ||
else { | ||
return PhpCompletionTokens.formatTokensDoc.getOrDefault(tokenText, ""); | ||
} | ||
} | ||
|
||
@Override | ||
public @Nullable PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Object object, PsiElement psiElement) { | ||
if (!(object instanceof String)) { | ||
return null; | ||
} | ||
|
||
String fqn = getCallToFormatFunc(psiElement); | ||
if (StringUtil.isNullOrEmpty(fqn)) { | ||
return null; | ||
} | ||
|
||
String tokenText = (String)object; | ||
|
||
if ("%%".equals(tokenText)) { | ||
tokenText = "%"; | ||
} | ||
else if (!"%".equals(tokenText)) { | ||
tokenText = StringUtils.strip((String)object, "%"); | ||
} | ||
String functionName = StringUtils.strip(fqn, "\\"); | ||
return new FormatTokenDocElement(psiManager, psiElement.getLanguage(), tokenText, functionName); | ||
} | ||
|
||
private String getCallToFormatFunc(PsiElement psiElement) { | ||
FunctionReference function = PhpPsiUtil.getParentByCondition(psiElement, true, FunctionReference.INSTANCEOF, Statement.INSTANCEOF); | ||
if (function == null) { | ||
return null; | ||
} | ||
return PhpElementsUtil.resolveFqn(function); | ||
} | ||
|
||
private static class FormatTokenDocElement extends LightElement { | ||
|
||
private final String tokenText; | ||
private final String functionName; | ||
|
||
protected FormatTokenDocElement(@NotNull final PsiManager manager, @NotNull final Language language, @NotNull final String tokenText, @NotNull final String functionName) { | ||
super(manager, language); | ||
this.tokenText = tokenText; | ||
this.functionName = functionName; | ||
} | ||
|
||
public String getTokenText() { | ||
return tokenText; | ||
} | ||
|
||
@Override | ||
public String getText() { | ||
return functionName; | ||
} | ||
|
||
public String getFunctionName() { | ||
return functionName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "FormatTokenDocElement for " + tokenText; | ||
} | ||
} | ||
} |
Oops, something went wrong.