Code review comment: Instead of handling build projects inside the catch block, cant we first check for the project root and, branch out into the two separate logics(single file and package) based on the output?

Code before review:
public static List<String> getExistingTypeNames(WorkspaceManager workspaceManager, String filePathUri) {
List<String> existingTypeNames = new ArrayList<>();
if (filePathUri == null) {
return existingTypeNames;
}
Path filePathResolved;
try {
URI filePathUriResolved = new URI(filePathUri);
filePathResolved = Path.of(filePathUriResolved);
} catch (InvalidPathException | URISyntaxException e) {
return existingTypeNames;
}
if (workspaceManager != null && workspaceManager.semanticModel(filePathResolved).isPresent()) {
List<Symbol> moduleSymbols = workspaceManager.semanticModel(filePathResolved).get().moduleSymbols();
moduleSymbols.forEach(symbol -> {
if (symbol.getName().isPresent()) {
existingTypeNames.add(symbol.getName().get());
}
});
return existingTypeNames;
}
Project project;
try {
project = SingleFileProject.load(filePathResolved);
List<Symbol> moduleSymbols =
project.currentPackage().getDefaultModule().getCompilation().getSemanticModel().moduleSymbols();
moduleSymbols.forEach(symbol -> {
if (symbol.getName().isPresent()) {
existingTypeNames.add(symbol.getName().get());
}
});
} catch (ProjectException pe) {
Path projectRoot = ProjectUtils.findProjectRoot(filePathResolved);
if (projectRoot != null) {
try {
project = BuildProject.load(projectRoot);
List<Symbol> moduleSymbols = project.currentPackage()
.module(project.documentId(filePathResolved).moduleId())
.getCompilation().getSemanticModel().moduleSymbols();
moduleSymbols.forEach(symbol -> {
if (symbol.getName().isPresent()) {
existingTypeNames.add(symbol.getName().get());
}
});
} catch (ProjectException pe1) {
return existingTypeNames;
}
}
}
return existingTypeNames;
}

Line under review: }

Code after addressing the review:
public static List<String> getExistingTypeNames(WorkspaceManager workspaceManager, String filePathUri) {
List<String> existingTypeNames = new ArrayList<>();
if (filePathUri == null) {
return existingTypeNames;
}
Path filePathResolved;
try {
URI filePathUriResolved = new URI(filePathUri);
filePathResolved = Path.of(filePathUriResolved);
} catch (InvalidPathException | URISyntaxException e) {
return existingTypeNames;
}
if (workspaceManager != null && workspaceManager.semanticModel(filePathResolved).isPresent()) {
List<Symbol> moduleSymbols = workspaceManager.semanticModel(filePathResolved).get().moduleSymbols();
moduleSymbols.forEach(symbol -> {
if (symbol.getName().isPresent()) {
existingTypeNames.add(symbol.getName().get());
}
});
return existingTypeNames;
}
try {
Project project;
List<Symbol> moduleSymbols;
Path projectRoot = ProjectUtils.findProjectRoot(filePathResolved);
if (projectRoot == null) {
project = SingleFileProject.load(filePathResolved);
moduleSymbols =
project.currentPackage().getDefaultModule().getCompilation().getSemanticModel().moduleSymbols();
moduleSymbols.forEach(symbol -> {
if (symbol.getName().isPresent()) {
existingTypeNames.add(symbol.getName().get());
}
});
} else {
project = BuildProject.load(projectRoot);
moduleSymbols = project.currentPackage()
.module(project.documentId(filePathResolved).moduleId())
.getCompilation().getSemanticModel().moduleSymbols();
moduleSymbols.forEach(symbol -> {
if (symbol.getName().isPresent()) {
existingTypeNames.add(symbol.getName().get());
}
});
}
} catch (ProjectException pe) {
return existingTypeNames;
}
return existingTypeNames;
}