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

KE2: basic external class extraction #18134

Open
wants to merge 9 commits into
base: ke2
Choose a base branch
from

Conversation

smowton
Copy link
Contributor

@smowton smowton commented Nov 27, 2024

Mainly just makes Kt classes optional to the point that basic external type extraction works, adds basic type parameter type extraction so we can at least tolerate seeing generics, and switches the external declaration extractor back on.

@smowton smowton requested a review from a team as a code owner November 27, 2024 15:33
fileExtractionProblems.setNonRecoverableProblem()
*/
loggerBase.error(dtw, "Extraction failed while extracting '${psiFile.virtualFilePath}'.", e.stackTraceToString())
Copy link
Contributor

Choose a reason for hiding this comment

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

We have a Logger.error(msg: String, exn: Throwable); it's probably more consistent to add one to LoggerBase too (and BasicLogger?) than to do the stackTraceToString here.

val name =
when (element) {
is KtFile -> element.virtualFilePath
is KtNamed -> element.getNameAsName()?.asString() ?: "<missing name>"
else -> "<no name>"
}
val loc = tw.getLocationString(element)
val loc = element?.let { tw.getLocationString(it) } ?: "<unknown location>"
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this is for the null declaration.psiSafe() case below. I think it would be better to generalise the element parameter, or to add a withSymbol alternative function, so that we only have non-null elements. After all, when this appears in a stack trace, it's more useful to know that we were extracting the symbol for the declaration, rather than believe we were extracting the source declaration itself (or worse, have no info on what we were extracting).

@@ -433,6 +430,10 @@ open class FileTrapWriter(
return getLocation(file, range)
}

fun getFileOnlyLocation(): Label<DbLocation> {
return getLocation(fileId, 0, 0, 0, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

This already exists as getWholeFileLocation

logger.warnElement("Unrecognised class kind $kind", e)
else
logger.warn("Unrecognised class kind $kind")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

A few thoughts on this:

No matter what, I think we should wrap whatever pattern we end up using in a logger.warnSymbol method.

Even when there isn't a psiSafe, I think we can still get at least a name from the symbol.

I'm not sure if using the location of psiSafe is going to be useful or not; we're not actually extracting the source at this point, so is giving the source location going to send us in the wrong direction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've gone for preserving psiSafe's location info if we can, since it is strictly more informative than symbol location. We can remove this if it proves not to be useful. I also note when inline generics are back in play we'll need to retrieve symbol locations differently since they won't always be extracted to the FileTrapWriter's fileId.

@@ -56,7 +62,7 @@ fun KotlinFileExtractor.extractClassSource(
}
}

val locId = tw.getLocation(c.psiSafe() ?: TODO())
val locId = c.psiSafe<KtElement>()?.let { tw.getLocation(it) } ?: tw.getFileOnlyLocation()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we want to do this. If we can see the source, then we'll get the location when we extract the source. If we're here, we should just give the whole-file location.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is that place, though? In KE1 at least this function was shared between the extract-kt-source-class and extract-class-file paths, and it is shared here too since it works at symbol level. I'm not aware of another place where the whole class would get a source-file location ascribed if we know of one.

return id
}

private fun tryReplaceType(
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment giving the purpose and behaviour of this method please?

*/
extractClassLaterIfExternal(replacedClass)
// TODO: This shouldn't be done here, but keeping it simple for now
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this comment?

@smowton smowton force-pushed the smowton/ke2/external-class-extraction branch from d2323e9 to d27b5ed Compare November 27, 2024 17:12
@smowton
Copy link
Contributor Author

smowton commented Nov 27, 2024

@igfoo comments addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants