From 0a07fe6b276d1277e5ff79983a84d446e92b5f74 Mon Sep 17 00:00:00 2001 From: paulklint Date: Mon, 25 Nov 2024 22:27:53 +0100 Subject: [PATCH] Added a similarNames field to TypePalConfig A user of TypePal can now control the way similar names are generated. --- src/analysis/typepal/ConfigurableScopeGraph.rsc | 5 +++++ src/analysis/typepal/Solver.rsc | 2 +- src/analysis/typepal/StringSimilarity.rsc | 15 ++++++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/analysis/typepal/ConfigurableScopeGraph.rsc b/src/analysis/typepal/ConfigurableScopeGraph.rsc index 0d8d923..3204020 100644 --- a/src/analysis/typepal/ConfigurableScopeGraph.rsc +++ b/src/analysis/typepal/ConfigurableScopeGraph.rsc @@ -15,6 +15,7 @@ import Map; import util::Reflective; import String; extend ParseTree; +import analysis::typepal::StringSimilarity; public loc anonymousOccurrence = |rascal-typepal:///anonymous_occurrence|(0,1,<2,3>,<2,4>); @@ -79,6 +80,8 @@ loc defaultLogicalLoc(Define def, str _modelName, PathConfig _pcfg){ return def.defined; // return original and don't create logical location } +list[str] defaultSimilarNames(Use u, TModel tm) = similarNames(u, tm); + // Extends TypePalConfig defined in analysis::typepal::ScopeGraph data TypePalConfig( @@ -139,6 +142,8 @@ data TypePalConfig( loc (Define def, str modelName, PathConfig pcfg) createLogicalLoc = defaultLogicalLoc, + list[str] (Use u, TModel tm) similarNames = defaultSimilarNames, + bool enableErrorFixes = true, int cutoffForNameSimilarity = 3 diff --git a/src/analysis/typepal/Solver.rsc b/src/analysis/typepal/Solver.rsc index 037dada..813d220 100644 --- a/src/analysis/typepal/Solver.rsc +++ b/src/analysis/typepal/Solver.rsc @@ -1722,5 +1722,5 @@ list[CodeAction] undefinedNameProposals(Use u, TModel tm) title="Replace undefined ``", edits=[changed([replace(u.occ, prop)])] ) - | str prop <- similarNames(getOrgId(u), u.idRoles, tm) + | str prop <- tm.config.similarNames(u, tm) ]; \ No newline at end of file diff --git a/src/analysis/typepal/StringSimilarity.rsc b/src/analysis/typepal/StringSimilarity.rsc index 25717ca..af44681 100644 --- a/src/analysis/typepal/StringSimilarity.rsc +++ b/src/analysis/typepal/StringSimilarity.rsc @@ -1,10 +1,12 @@ module analysis::typepal::StringSimilarity import List; +import IO; +import Location; import Set; import String; import analysis::typepal::TModel; -import analysis::typepal::ConfigurableScopeGraph; +import analysis::typepal::ConfigurableScopeGraph; @synopsis{Tryadic minimum function on integers} int min(int a, int b, int c) @@ -52,10 +54,13 @@ alias WordSim = tuple[str word, int sim]; @synopsis{Compute list of words from vocabulary, that are similar to give word w with at most maxDistance edits} list[str] similarWords(str w, list[str] vocabulary, int maxDistance) -= sort({ | str v <- vocabulary, d := lev(w, v), d <= maxDistance }, bool (WordSim x, WordSim y){ return x.sim < y.sim;}).word; += sort({ | str v <- vocabulary, d := lev(w, v), d <= maxDistance }, + bool (WordSim x, WordSim y){ return x.sim < y.sim;}).word; -@synopsis{Find in TModel tm, names similar to w, in gives roles. Max edit distance comes from TypePal Configuration.} -list[str] similarNames(str w, set[IdRole] idRoles, TModel tm){ - vocabulary = [ d.orgId | d <- tm.defines, d.idRole in idRoles ]; +@synopsis{Find in TModel tm, names similar to Use u. Max edit distance comes from TypePal Configuration.} +list[str] similarNames(Use u, TModel tm){ + w = getOrgId(u); + idRoles = u.idRoles; + vocabulary = [ d.orgId | d <- tm.defines, d.idRole in idRoles, isContainedIn(u.occ, d.scope) ]; return similarWords(w, vocabulary, tm.config.cutoffForNameSimilarity); }