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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations