Search in sources :

Example 1 with IErlRecordDef

use of org.erlide.engine.model.erlang.IErlRecordDef in project erlide_eclipse by erlang.

the class IndexedErlangValue method checkRecord.

private IErlRecordDef checkRecord(final OtpErlangObject o) {
    if (o instanceof OtpErlangTuple) {
        final OtpErlangTuple t = (OtpErlangTuple) o;
        final OtpErlangObject h = t.elementAt(0);
        if (h instanceof OtpErlangAtom) {
            final OtpErlangAtom a = (OtpErlangAtom) h;
            final ErlangDebugTarget target = getErlangDebugTarget();
            IErlPreprocessorDef pd;
            try {
                pd = ErlangEngine.getInstance().getModelFindService().findPreprocessorDef(getErlProjects(target.getProjects()), moduleName, a.atomValue(), ErlElementKind.RECORD_DEF);
                if (pd instanceof IErlRecordDef) {
                    final IErlRecordDef r = (IErlRecordDef) pd;
                    if (r.hasChildren() && r.getChildCount() + 1 == t.arity()) {
                        return r;
                    }
                }
            } catch (final CoreException e) {
            }
        }
    }
    return null;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IErlRecordDef(org.erlide.engine.model.erlang.IErlRecordDef) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) IErlPreprocessorDef(org.erlide.engine.model.erlang.IErlPreprocessorDef)

Example 2 with IErlRecordDef

use of org.erlide.engine.model.erlang.IErlRecordDef in project erlide_eclipse by erlang.

the class ErlParser method addRecordDef.

private IErlRecordDef addRecordDef(final IErlModule module, final OtpErlangObject pos, final OtpErlangObject val, final OtpErlangObject extra) {
    if (val instanceof OtpErlangTuple) {
        final OtpErlangTuple recordTuple = (OtpErlangTuple) val;
        if (recordTuple.elementAt(0) instanceof OtpErlangAtom) {
            final String s = extra instanceof OtpErlangString ? ((OtpErlangString) extra).stringValue() : null;
            final OtpErlangList fields = (OtpErlangList) recordTuple.elementAt(1);
            final ErlRecordDef r = new ErlRecordDef(module, null, s);
            setPos(r, pos);
            if (fields != null) {
                final List<ErlRecordField> children = Lists.newArrayListWithCapacity(fields.arity());
                for (final OtpErlangObject o : fields) {
                    if (o instanceof OtpErlangTuple) {
                        final OtpErlangTuple fieldTuple = (OtpErlangTuple) o;
                        final OtpErlangAtom fieldNameAtom = (OtpErlangAtom) fieldTuple.elementAt(0);
                        final String fieldName = fieldNameAtom.atomValue();
                        final ErlRecordField field = new ErlRecordField(r, fieldName);
                        final OtpErlangTuple posTuple = (OtpErlangTuple) fieldTuple.elementAt(1);
                        if (fieldTuple.arity() > 2) {
                            final OtpErlangObject fieldExtra = fieldTuple.elementAt(2);
                            field.setExtra(Util.stringValue(fieldExtra));
                        }
                        setPos(field, posTuple);
                        children.add(field);
                    } else {
                        ErlLogger.error("bad record def: %s", o);
                    }
                }
                r.setChildren(children);
            } else {
                r.setChildren(new ArrayList<>());
            }
            return r;
        }
    }
    if (val instanceof OtpErlangAtom) {
        final String s = extra instanceof OtpErlangString ? ((OtpErlangString) extra).stringValue() : null;
        final ErlRecordDef r = new ErlRecordDef(module, null, s);
        setPos(r, pos);
        return r;
    }
    return null;
}
Also used : OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) ErlRecordField(org.erlide.engine.internal.model.erlang.ErlRecordField) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) ErlRecordDef(org.erlide.engine.internal.model.erlang.ErlRecordDef) IErlRecordDef(org.erlide.engine.model.erlang.IErlRecordDef) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 3 with IErlRecordDef

use of org.erlide.engine.model.erlang.IErlRecordDef in project erlide_eclipse by erlang.

the class SearchPatternFactory method getSearchPatternFromErlElementAndLimitTo.

public ErlangSearchPattern getSearchPatternFromErlElementAndLimitTo(final IErlElement element, final LimitTo limitTo) {
    if (element instanceof IErlFunction) {
        final IErlFunction function = (IErlFunction) element;
        final String withoutExtension = SystemConfiguration.withoutExtension(function.getModuleName());
        return new FunctionPattern(withoutExtension, function.getFunctionName(), function.getArity(), limitTo, true, modelUtilService.getModule(function), !function.isExported());
    } else if (element instanceof IErlMacroDef) {
        final IErlMacroDef m = (IErlMacroDef) element;
        final String unquoted = StringUtils.unquote(m.getDefinedName());
        return new MacroPattern(unquoted, limitTo);
    } else if (element instanceof IErlRecordDef) {
        final IErlRecordDef r = (IErlRecordDef) element;
        final String unquoted = StringUtils.unquote(r.getDefinedName());
        return new RecordPattern(unquoted, limitTo);
    } else if (element instanceof IErlFunctionClause) {
        final IErlFunctionClause clause = (IErlFunctionClause) element;
        return getSearchPatternFromErlElementAndLimitTo((IErlElement) clause.getParent(), limitTo);
    } else if (element instanceof IErlAttribute) {
        final IErlAttribute a = (IErlAttribute) element;
        if (a.getName().startsWith("include")) {
            final String s = Util.stringValue(a.getValue());
            return new IncludePattern(s, limitTo);
        }
    }
    return null;
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) IErlRecordDef(org.erlide.engine.model.erlang.IErlRecordDef) IErlFunction(org.erlide.engine.model.erlang.IErlFunction) IErlAttribute(org.erlide.engine.model.erlang.IErlAttribute) IErlMacroDef(org.erlide.engine.model.erlang.IErlMacroDef) IErlFunctionClause(org.erlide.engine.model.erlang.IErlFunctionClause)

Example 4 with IErlRecordDef

use of org.erlide.engine.model.erlang.IErlRecordDef in project erlide_eclipse by erlang.

the class ModelUtilsTest method findPreprocessorDefTest.

@Test
public void findPreprocessorDefTest() throws Exception {
    // given
    // a module with includes and record
    final IErlProject project = ModelUtilsTest.projects[0];
    final IErlModule include = ErlideTestUtils.createInclude(project, "a.hrl", "-record(rec1, {field, another=def}).\n-define(MACRO(A), lists:reverse(A)).\n");
    final IErlModule module = ErlideTestUtils.createModule(project, "f.erl", "-module(f).\n-include(\"a.hrl\").\n-export([f/0]).\n-record(rec2, {a, b}).\n" + "f() ->\n    lists:reverse([1, 0]),\n    lists:reverse([1, 0], [2]).\n");
    module.open(null);
    project.open(null);
    final IErlPreprocessorDef preprocessorDef1 = modelFindService.findPreprocessorDef(module, "rec1", ErlElementKind.RECORD_DEF);
    final IErlPreprocessorDef preprocessorDef2 = modelFindService.findPreprocessorDef(include, "rec1", ErlElementKind.RECORD_DEF);
    final IErlPreprocessorDef preprocessorDef3 = modelFindService.findPreprocessorDef(Arrays.asList(ModelUtilsTest.projects), "f.erl", "rec2", ErlElementKind.RECORD_DEF);
    // then
    // the record should be returned
    assertNotNull(module);
    assertNotNull(preprocessorDef1);
    assertTrue(preprocessorDef1 instanceof IErlRecordDef);
    assertEquals(preprocessorDef1, preprocessorDef2);
    assertEquals(preprocessorDef1.getParent(), include);
    assertNotNull(preprocessorDef3);
    assertEquals(preprocessorDef3.getParent(), module);
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IErlRecordDef(org.erlide.engine.model.erlang.IErlRecordDef) IErlModule(org.erlide.engine.model.root.IErlModule) IErlPreprocessorDef(org.erlide.engine.model.erlang.IErlPreprocessorDef) Test(org.junit.Test)

Example 5 with IErlRecordDef

use of org.erlide.engine.model.erlang.IErlRecordDef in project erlide_eclipse by erlang.

the class ModelUtilsTest method findPreprocessorDefOtpIncludeTest.

@Test
public void findPreprocessorDefOtpIncludeTest() throws Exception {
    // given
    // a module with includes and record
    final IErlProject project = ModelUtilsTest.projects[0];
    final IErlModule module = ErlideTestUtils.createModule(project, "g.erl", "-module(g).\n-include_lib(\"kernel/include/file.hrl\").\n-export([f/0]).\n-record(rec2, {a, b}).\n" + "f() ->\n    lists:reverse([1, 0]),\n    lists:reverse([1, 0], [2]).\n");
    module.open(null);
    // when
    // looking for the record
    final IErlPreprocessorDef preprocessorDef = modelFindService.findPreprocessorDef(module, "file_info", ErlElementKind.RECORD_DEF);
    // then
    // the record should be returned
    assertNotNull(module);
    assertNotNull(preprocessorDef);
    assertTrue(preprocessorDef instanceof IErlRecordDef);
    assertEquals(ErlangEngine.getInstance().getModelUtilService().getProject(preprocessorDef), project);
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IErlRecordDef(org.erlide.engine.model.erlang.IErlRecordDef) IErlModule(org.erlide.engine.model.root.IErlModule) IErlPreprocessorDef(org.erlide.engine.model.erlang.IErlPreprocessorDef) Test(org.junit.Test)

Aggregations

IErlRecordDef (org.erlide.engine.model.erlang.IErlRecordDef)7 IErlPreprocessorDef (org.erlide.engine.model.erlang.IErlPreprocessorDef)4 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)3 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)3 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)2 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)2 CoreException (org.eclipse.core.runtime.CoreException)2 IErlElement (org.erlide.engine.model.IErlElement)2 IErlModule (org.erlide.engine.model.root.IErlModule)2 IErlProject (org.erlide.engine.model.root.IErlProject)2 Test (org.junit.Test)2 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)1 ArrayList (java.util.ArrayList)1 ErlRecordDef (org.erlide.engine.internal.model.erlang.ErlRecordDef)1 ErlRecordField (org.erlide.engine.internal.model.erlang.ErlRecordField)1 ErlElementKind (org.erlide.engine.model.ErlElementKind)1 ErlModelException (org.erlide.engine.model.ErlModelException)1 IErlAttribute (org.erlide.engine.model.erlang.IErlAttribute)1 IErlFunction (org.erlide.engine.model.erlang.IErlFunction)1 IErlFunctionClause (org.erlide.engine.model.erlang.IErlFunctionClause)1