Skip to content

Commit 32a9aeb

Browse files
committed
Fix bug where Info namespace had overlap with requested name
1 parent 264c96a commit 32a9aeb

File tree

6 files changed

+58
-44
lines changed

6 files changed

+58
-44
lines changed

src/Code/CodebaseTree.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ viewDefinitionListing openDefinitions listing =
241241
"Term listing: " ++ FQN.toString fqn ++ "\n"
242242

243243
b =
244-
"OpenDefinitions" ++ (openDefinitions |> FQNSet.toList |> List.map FQN.toString |> String.join ", ")
244+
"OpenDefinitions: " ++ (openDefinitions |> FQNSet.toList |> List.map FQN.toString |> String.join ", ")
245245
in
246246
ProdDebug.view (a ++ b)
247247
[ viewDefRow (TermReference (NameOnly fqn)) fqn (Category.name category) (Category.icon category)

src/Code/Definition/Info.elm

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ makeInfo ref suffixName allFqns_ =
3030
Info suffixName namespace otherNames
3131

3232

33+
34+
-- Helpers
35+
36+
3337
allFqns : Info -> List FQN
3438
allFqns info =
3539
let
@@ -41,10 +45,6 @@ allFqns info =
4145
ListE.uniqueBy FQN.toString (reconstructed :: info.otherNames)
4246

4347

44-
45-
-- Helpers
46-
47-
4848
namespaceAndOtherNames : Reference -> FQN -> NEL.Nonempty FQN -> ( Maybe FQN, List FQN )
4949
namespaceAndOtherNames requestedRef suffixName fqns =
5050
let
@@ -60,22 +60,26 @@ namespaceAndOtherNames requestedRef suffixName fqns =
6060
|> NEL.filter (FQN.isSuffixOf suffixName) defaultFqn
6161
|> shortest
6262

63-
fqnWithin =
63+
( namespace, fqnWithin ) =
6464
case Reference.fqn requestedRef of
6565
Just requestedName ->
6666
if FQN.isSuffixOf suffixName requestedName then
67-
requestedName
67+
let
68+
toTake =
69+
FQN.length requestedName - FQN.length suffixName
70+
in
71+
( Just (FQN.take toTake requestedName), shortestSuffixMatching )
6872

6973
else
70-
shortestSuffixMatching
74+
( FQN.namespace shortestSuffixMatching, shortestSuffixMatching )
7175

7276
Nothing ->
73-
shortestSuffixMatching
77+
( FQN.namespace shortestSuffixMatching, shortestSuffixMatching )
7478

7579
fqnsWithout =
7680
fqns
7781
|> NEL.toList
7882
|> ListE.filterNot (FQN.equals fqnWithin)
7983
|> ListE.uniqueBy FQN.toString
8084
in
81-
( FQN.namespace fqnWithin, fqnsWithout )
85+
( namespace, fqnsWithout )

src/Code/FullyQualifiedName.elm

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ module Code.FullyQualifiedName exposing
1717
, isSuffixOf
1818
, isValidSegmentChar
1919
, isValidUrlSegmentChar
20+
, length
2021
, namespace
2122
, namespaceOf
2223
, numSegments
2324
, root
2425
, segments
2526
, snoc
2627
, stripPrefix
28+
, take
2729
, toApiUrlString
2830
, toQueryString
2931
, toQueryString_
@@ -183,6 +185,11 @@ numSegments (FQN segments_) =
183185
NEL.length segments_
184186

185187

188+
length : FQN -> Int
189+
length (FQN segments_) =
190+
NEL.length segments_
191+
192+
186193
{-| Drops the last segment of the FQN, unless there's only 1
187194
-}
188195
dropLast : FQN -> FQN
@@ -194,6 +201,11 @@ dropLast (FQN segments_) =
194201
)
195202

196203

204+
take : Int -> FQN -> FQN
205+
take n (FQN segments_) =
206+
FQN (NEL.take n segments_)
207+
208+
197209
fromParent : FQN -> String -> FQN
198210
fromParent (FQN parentParts) childName =
199211
FQN (NEL.append parentParts (NEL.fromElement childName))
@@ -241,15 +253,10 @@ stripPrefix (FQN prefix) (FQN fqn_) =
241253

242254
fqn__ =
243255
NEL.toList fqn_
244-
245-
potentialPrefix =
246-
List.take (List.length prefix_) fqn__
247256
in
248-
if prefix_ == potentialPrefix then
249-
fromList (List.drop (List.length prefix_) fqn__)
250-
251-
else
252-
fromList fqn__
257+
ListE.stripPrefix prefix_ fqn__
258+
|> Maybe.withDefault fqn__
259+
|> fromList
253260

254261

255262
{-| Extend a FQN with another, removing exact overlaps in the right side list.
@@ -290,13 +297,13 @@ Check if the second FQN ends with the first FQN.
290297
291298
-}
292299
isSuffixOf : FQN -> FQN -> Bool
293-
isSuffixOf suffixName fqn =
294-
String.endsWith (toString suffixName) (toString fqn)
300+
isSuffixOf (FQN suffixName) (FQN fqn) =
301+
ListE.isSuffixOf (NEL.toList suffixName) (NEL.toList fqn)
295302

296303

297304
isPrefixOf : FQN -> FQN -> Bool
298-
isPrefixOf prefixName fqn =
299-
String.startsWith (toString prefixName) (toString fqn)
305+
isPrefixOf (FQN prefixName) (FQN fqn) =
306+
ListE.isPrefixOf (NEL.toList prefixName) (NEL.toList fqn)
300307

301308

302309
isRoot : FQN -> Bool

src/Lib/ProdDebug.elm

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
port module Lib.ProdDebug exposing (..)
1+
module Lib.ProdDebug exposing (..)
22

33
import Html exposing (Html, node)
44
import Html.Attributes exposing (attribute)
@@ -7,21 +7,3 @@ import Html.Attributes exposing (attribute)
77
view : String -> List (Html msg) -> Html msg
88
view debugMessage content =
99
node "prod-debug" [ attribute "message" debugMessage ] content
10-
11-
12-
{-| This requires a port subscription by the host application.
13-
14-
Something like:
15-
16-
```javascript
17-
const app = Elm.HostApp.init({ flags });
18-
19-
if (app.ports) {
20-
app.ports.debugLog?.subscribe((text) => {
21-
console.debug(text);
22-
});
23-
}
24-
```
25-
26-
-}
27-
port debugLog : String -> Cmd msg

tests/Code/Definition/InfoTests.elm

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,26 @@ namespaceAndOtherNames =
3030
[ "base.List.map", "something.else.List.map" ]
3131
in
3232
Expect.equal expected otherNames
33+
, test "does not have overlap in namespace" <|
34+
\_ ->
35+
let
36+
result =
37+
Info.namespaceAndOtherNames
38+
(Reference.fromString TermReference "BlogAuthor.bio.modify")
39+
(FQN.fromString "bio.modify")
40+
(NEL.Nonempty
41+
(FQN.fromString "BlogAuthor.bio.modify")
42+
[ FQN.fromString "Bio.change" ]
43+
)
44+
45+
namespace =
46+
Tuple.first result |> Maybe.map FQN.toString
47+
48+
otherNames =
49+
Tuple.second result |> List.map FQN.toString
50+
51+
expected =
52+
( Just "BlogAuthor", [ "Bio.change" ] )
53+
in
54+
Expect.equal expected ( namespace, otherNames )
3355
]

tests/Code/FullyQualifiedNameSetTests.elm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module Code.FullyQualifiedNameSetTests exposing (..)
22

3-
import Code.FullyQualifiedName as FQN exposing (..)
4-
import Code.FullyQualifiedNameSet as FQNSet exposing (..)
3+
import Code.FullyQualifiedName as FQN
4+
import Code.FullyQualifiedNameSet as FQNSet
55
import Expect
6-
import List.Nonempty as NEL
76
import Test exposing (..)
87

98

0 commit comments

Comments
 (0)