Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reference handler #11

Merged
merged 8 commits into from
Oct 12, 2023
Merged

Conversation

bryankenote
Copy link
Contributor

@bryankenote bryankenote commented Oct 11, 2023

Based on #10. Will open once that is merged.

@bryankenote bryankenote added enhancement New feature or request help wanted Extra attention is needed labels Oct 11, 2023
@bryankenote bryankenote force-pushed the reference-handler branch 2 times, most recently from 97bd216 to 21b7d26 Compare October 11, 2023 18:32
@bryankenote bryankenote removed the help wanted Extra attention is needed label Oct 11, 2023
@bryankenote bryankenote force-pushed the reference-handler branch 3 times, most recently from 6301855 to acb4e81 Compare October 11, 2023 19:47
@bryankenote bryankenote marked this pull request as ready for review October 11, 2023 23:13
Comment on lines 20 to 65
public static IEnumerable<ServicePart> GetReferencedServicePartsAtPosition(this ServiceInfo service, Position requestPosition)
{
var members = service.GetDescendants().OfType<ServiceMemberInfo>().ToList().AsReadOnly();

// memberNameAtCursor will be null if the cursor is not on a member name.
var memberNameAtCursor = members
.Select(member =>
{
var part = member.GetPart(ServicePartKind.Name);
var name = member.Name;

return (part, name);
})
.Where(x => x.part != null && requestPosition >= x.part.Position && requestPosition < x.part.EndPosition)
.Select(x => x.name)
.FirstOrDefault();

var fields = service.GetDescendants().OfType<ServiceFieldInfo>().ToList().AsReadOnly();

// fieldTypeNameAtCursor will be null if the cursor is not on a field type name.
var fieldTypeNameAtCursor = fields
.Select(field =>
{
var part = field.GetPart(ServicePartKind.TypeName);
var typeName = service.GetFieldTypeName(field);

return (part, typeName);
})
.Where(x => x.part != null && requestPosition >= x.part.Position && requestPosition < x.part.EndPosition)
.Select(x => x.typeName)
.FirstOrDefault();

return fields
.Select(field =>
{
var part = field.GetPart(ServicePartKind.TypeName);
var typeName = service.GetFieldTypeName(field);

var type = service.GetFieldType(field);
var memberTypeName = type?.GetMemberTypeName();

return (part, memberTypeName, typeName);
})
.Where(x => x.part != null && ((memberNameAtCursor != null && x.memberTypeName == memberNameAtCursor) || x.typeName == fieldTypeNameAtCursor))
.Select(x => x.part);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be rewritten using the query syntax if that is preferred. I originally used the method syntax because it was easier to debug.

Suggested change
public static IEnumerable<ServicePart> GetReferencedServicePartsAtPosition(this ServiceInfo service, Position requestPosition)
{
var members = service.GetDescendants().OfType<ServiceMemberInfo>().ToList().AsReadOnly();
// memberNameAtCursor will be null if the cursor is not on a member name.
var memberNameAtCursor = members
.Select(member =>
{
var part = member.GetPart(ServicePartKind.Name);
var name = member.Name;
return (part, name);
})
.Where(x => x.part != null && requestPosition >= x.part.Position && requestPosition < x.part.EndPosition)
.Select(x => x.name)
.FirstOrDefault();
var fields = service.GetDescendants().OfType<ServiceFieldInfo>().ToList().AsReadOnly();
// fieldTypeNameAtCursor will be null if the cursor is not on a field type name.
var fieldTypeNameAtCursor = fields
.Select(field =>
{
var part = field.GetPart(ServicePartKind.TypeName);
var typeName = service.GetFieldTypeName(field);
return (part, typeName);
})
.Where(x => x.part != null && requestPosition >= x.part.Position && requestPosition < x.part.EndPosition)
.Select(x => x.typeName)
.FirstOrDefault();
return fields
.Select(field =>
{
var part = field.GetPart(ServicePartKind.TypeName);
var typeName = service.GetFieldTypeName(field);
var type = service.GetFieldType(field);
var memberTypeName = type?.GetMemberTypeName();
return (part, memberTypeName, typeName);
})
.Where(x => x.part != null && ((memberNameAtCursor != null && x.memberTypeName == memberNameAtCursor) || x.typeName == fieldTypeNameAtCursor))
.Select(x => x.part);
}
public static IEnumerable<ServicePart> GetReferencedServicePartsAtPosition(this ServiceInfo service, Position requestPosition)
{
// memberNameAtCursor will be null if the cursor is not on a member name.
var memberNameAtCursor = (
from member in service.GetDescendants().OfType<ServiceMemberInfo>()
let part = member.GetPart(ServicePartKind.Name)
let memberName = member.Name
where part != null && requestPosition >= part.Position && requestPosition < part.EndPosition
select memberName).FirstOrDefault();
var fields = service.GetDescendants().OfType<ServiceFieldInfo>().ToList().AsReadOnly();
// fieldTypeNameAtCursor will be null if the cursor is not on a member name.
var fieldTypeNameAtCursor = (
from field in fields
let part = field.GetPart(ServicePartKind.TypeName)
let typeName = service.GetFieldTypeName(field)
where part != null && requestPosition >= part.Position && requestPosition < part.EndPosition
select typeName).FirstOrDefault();
return
from field in fields
let part = field.GetPart(ServicePartKind.TypeName)
let typeName = service.GetFieldTypeName(field)
let type = service.GetFieldType(field)
let memberTypeName = type?.GetMemberTypeName()
where part != null && ((memberNameAtCursor != null && memberTypeName == memberNameAtCursor) || typeName == fieldTypeNameAtCursor)
select part;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does read better, especially if you move the where part != null to immediately after let part = lines, but I don't feel strongly about it.

@bryankenote bryankenote merged commit 74adad6 into FacilityApi:master Oct 12, 2023
3 checks passed
@bryankenote bryankenote deleted the reference-handler branch October 12, 2023 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants