@Nullable private PsiElement findElementAt(@Nullable PsiFile file, int offset) { return file != null ? file.findElementAt(offset) : null; }
/** * If on the start/end of a block comment, jump to the matching of that comment, or vice versa. */ private static int findMatchingComment(@NotNull Editor editor, int pos) { final PsiFile psiFile = PsiHelper.getFile(editor); if (psiFile != null) { final PsiElement element = psiFile.findElementAt(pos); if (element != null) { return findMatchingBlockCommentPair(element, pos); } } return -1; }
@Nullable private static GoArgumentList getList(@NotNull ParameterInfoContext context) { PsiElement at = context.getFile().findElementAt(context.getOffset()); return PsiTreeUtil.getParentOfType(at, GoArgumentList.class); }
@Override public boolean isInContext(@NotNull PsiFile file, int offset) { if (PsiUtilCore.getLanguageAtOffset(file, offset).isKindOf(BallerinaLanguage.INSTANCE)) { PsiElement element = file.findElementAt(offset); return !(element instanceof PsiWhiteSpace) && element != null && isInContext(element); } return false; }
@Nullable private static GoExpression findExpressionAtOffset(GoIntroduceOperation operation) { PsiFile file = operation.getFile(); int offset = operation.getEditor().getCaretModel().getOffset(); GoExpression expr = PsiTreeUtil.getNonStrictParentOfType(file.findElementAt(offset), GoExpression.class); GoExpression preExpr = PsiTreeUtil.getNonStrictParentOfType(file.findElementAt(offset - 1), GoExpression.class); if (expr == null || preExpr != null && PsiTreeUtil.isAncestor(expr, preExpr, false)) return preExpr; return expr; }
@Override public boolean isInContext(@NotNull PsiFile file, int offset) { if (PsiUtilCore.getLanguageAtOffset(file, offset).isKindOf(GoLanguage.INSTANCE)) { PsiElement psiElement = ObjectUtils.notNull(file.findElementAt(offset), file); if (!acceptLeaf()) { psiElement = getFirstCompositeElement(psiElement); } return psiElement != null && isInContext(psiElement); } return false; }
@NotNull private ConfigurationContext createConfigurationContext() { PsiElement at = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); assertNotNull(at); return new ConfigurationContext(at); }
@Override public void handleInsert(@NotNull InsertionContext context, LookupElement item) { PsiFile file = context.getFile(); if (!(file instanceof GoFile)) return; context.commitDocument(); int offset = context.getStartOffset(); PsiElement at = file.findElementAt(offset); GoCompositeElement ref = PsiTreeUtil.getParentOfType(at, GoValue.class, GoReferenceExpression.class); if (ref instanceof GoReferenceExpression && (((GoReferenceExpression)ref).getQualifier() != null || GoPsiImplUtil.prevDot(ref))) { return; } GoValue value = PsiTreeUtil.getParentOfType(at, GoValue.class); if (value == null || PsiTreeUtil.getPrevSiblingOfType(value, GoKey.class) != null) return; super.handleInsert(context, item); } };
private void doTest(@NotNull String expressionText, @NotNull ReadWriteAccessDetector.Access expectedAccess) { myFixture.configureByText("a.go", "package main; func _() {\n" + expressionText + "\n}"); PsiElement element = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); GoReferenceExpression expression = PsiTreeUtil.getNonStrictParentOfType(element, GoReferenceExpression.class); assertNotNull(expression); assertEquals(expectedAccess, expression.getReadWriteAccess()); } }
private <T extends GoExpression> void doTest(@NotNull String text, @NotNull Class<T> aClass, int expected) { myFixture.configureByText("a.go", text); PsiElement element = myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset()); T callExpr = PsiTreeUtil.getParentOfType(element, aClass); assertEquals(expected, GoInspectionUtil.getExpressionResultCount(callExpr)); }
@Override protected void doTest() { PsiFile file = myFixture.configureByText("a.go", "package main\n func foo(i interface{}, j interface{}){}\n" + vars + "\n func _(){\n fo<caret>o(" + left + ", " + right + ")\n}"); myFixture.checkHighlighting(); GoCallExpr call = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), GoCallExpr.class); assert call != null; List<GoExpression> expressions = call.getArgumentList().getExpressionList(); assertTrue(left + " should " + (ok ? "" : "not ") + "be identical " + right, ok == GoExpressionUtil.identical(expressions.get(0), expressions.get(1))); } }
@NotNull protected PsiElement findElementAtCaretOrInSelection() { SelectionModel selectionModel = myFixture.getEditor().getSelectionModel(); if (selectionModel.hasSelection()) { PsiElement left = myFixture.getFile().findElementAt(selectionModel.getSelectionStart()); PsiElement right = myFixture.getFile().findElementAt(selectionModel.getSelectionEnd() - 1); assertNotNull(left); assertNotNull(right); return ObjectUtils.assertNotNull(PsiTreeUtil.findCommonParent(left, right)); } else { return ObjectUtils.assertNotNull(myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset())); } }
public void testPackage_test() { PsiFile file = myFixture.addFileToProject("import/path/package_test.go", "package ma<caret>in; import `gopkg.in/check.v1`"); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); doTestProducedConfigurations(myFixture.getFile().findElementAt(myFixture.getCaretOffset())); }
public void testGoIndexOrSliceExprGetIndices() { PsiFile file = myFixture.configureByText("a.go", "package main\n var a []int\n var b = a<caret>[1]"); GoIndexOrSliceExpr index = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), GoIndexOrSliceExpr.class); assertNotNull(index); assertEquals("1", index.getIndices().first.getText()); }
private void doTest(@NotNull String text, String expected) { myFixture.configureByText("a.go", "package a\n" + text); PsiElement element = myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset()); GoType type = PsiTreeUtil.getParentOfType(element, GoType.class); assertNotNull(type); assertEquals(expected, type.getUnderlyingType().getText()); }
protected void doTestProducedConfigurations() { VirtualFile file = myFixture.copyFileToProject(getTestName(true) + ".go", "import/path/" + getTestName(true) + ".go"); myFixture.configureFromExistingVirtualFile(file); doTestProducedConfigurations(myFixture.getFile().findElementAt(myFixture.getCaretOffset())); }
public void testFieldInInitializer() { myFixture.configureByText("a.go", "package main\n" + "type aStruct struct {\n" + "\taField string\n" + "}\n" + "func newStruct() aStruct {\n" + "\treturn aStruct{\n" + "\t\taFie<caret>ld: \"a value\",\n" + "\t}\n" + "}"); PsiElement element = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); GoFieldName goFieldName = PsiTreeUtil.getNonStrictParentOfType(element, GoFieldName.class); assertNotNull(goFieldName); //noinspection ConstantConditions assertEquals(Write, ReadWriteAccessDetector.findDetector(goFieldName.getReference().resolve()).getExpressionAccess(goFieldName)); }
public void testSameRunApplicationConfigurationOnFile() { PsiFile file = myFixture.configureByText("a.go", "package main; <caret>\nfunc main() {}"); PsiElement at = file.findElementAt(myFixture.getCaretOffset()); assertNotNull(at); ConfigurationContext configurationContext = new ConfigurationContext(at); GoRunFileConfigurationProducer producer = new GoRunFileConfigurationProducer(); GoRunFileConfiguration runConfiguration = createFileConfiguration(file.getVirtualFile().getPath()); assertTrue(producer.isConfigurationFromContext(runConfiguration, configurationContext)); runConfiguration = createFileConfiguration(file.getVirtualFile().getPath() + "_vl"); assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext)); }
public void testSameRunFileConfigurationOnFile() { PsiFile file = myFixture.configureByText("a.go", "package main; <caret>\nfunc main() {}"); PsiElement at = file.findElementAt(myFixture.getCaretOffset()); assertNotNull(at); ConfigurationContext configurationContext = new ConfigurationContext(at); GoRunFileConfigurationProducer producer = new GoRunFileConfigurationProducer(); GoRunFileConfiguration runConfiguration = createFileConfiguration(file.getVirtualFile().getPath()); assertTrue(producer.isConfigurationFromContext(runConfiguration, configurationContext)); runConfiguration = createFileConfiguration(file.getVirtualFile().getPath() + "_vl"); assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext)); }
public void testSameRunApplicationConfigurationOnPackage() { PsiFile file = myFixture.configureByText("a.go", "package main; <caret>\nfunc main() {}"); PsiElement at = file.findElementAt(myFixture.getCaretOffset()); assertNotNull(at); ConfigurationContext configurationContext = new ConfigurationContext(at); GoApplicationRunConfigurationProducer producer = new GoApplicationRunConfigurationProducer(); GoApplicationConfiguration runConfiguration = createRunAppFileConfiguration(file.getVirtualFile().getPath()); assertTrue(producer.isConfigurationFromContext(runConfiguration, configurationContext)); runConfiguration = createRunAppFileConfiguration(file.getVirtualFile().getPath() + "_vl"); assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext)); }