Skip to content

Commit

Permalink
Fix: duplicated top-level node in $CodeCells
Browse files Browse the repository at this point in the history
This happened when the code cell only contains one top-level node whose syntax tree cannot be splitted.
  • Loading branch information
kenkangxgwe committed Sep 21, 2022
1 parent f6ab8d2 commit b1aaaa8
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions src/WolframLanguageServer/TextDocument.wl
Original file line number Diff line number Diff line change
Expand Up @@ -578,45 +578,51 @@ rangeToSyntaxTree[doc_TextDocument, ranges:{{_Integer, _Integer}...}] := With[
] // Part[#, 2]&
// Drop[#, First[range] - 1]& (* Drop extra Newlines *)
},
splitSyntaxTree[cst]
{range, splitSyntaxTree[cst]}
],
KeyValuePattern[{"cst" -> _Hold, "ast" -> _Hold}] :> (
range -> (syntaxTrees // Map[ReleaseHold /* Replace[err:Except[_List]:> LogError[err]]])
),
_ :> (range -> Missing["Omitted"] (* Value should not be used *))
KeyValuePattern[{"cst" -> _Hold, "ast" -> _Hold}] :> {
range,
{
range -> (
syntaxTrees
// Map[
ReleaseHold
/* Replace[err:Except[_List]:> LogError[err]]
]
)
}
},
_ -> Nothing
}]
)]
// Flatten
// If[uri // MissingQ,
Values,
newRules \[Function] With[
{
dropRanges = Complement[oldRanges, newRules // Keys],
addRanges = newRules // DeleteMissing // Keys
},

(* {"oldRanges:", oldRanges} // LogDebug; *)
(* {"newRanges:", newRules // Keys} // LogDebug; *)
dropRanges
// Map[range \[Function] (
Part[#, All, -1]&
/* Catenate
/* Values,
{
Part[#, All ,1]&
/* Map[range \[Function] (
$CodeCells[uri]
// KeySelect[IntervalMemberQ[#, range // Interval]&]
// Map[DeleteCases[range // Interval]]
// AssociateTo[$CodeCells[uri], #]&
)];
addRanges
// Map[range \[Function] (
// AssociateTo[$CodeCells[uri], #]&;
KeyDropFrom[$CodeRange[uri], Key[range]];
)],
Part[#, All, -1]&
/* Catenate
/* Map[Apply[{range, syntaxTree} \[Function] (
$CodeCells[uri]
// KeySelect[IntervalMemberQ[#, range // Interval]&]
// Map[Append[range // Interval]]
// AssociateTo[$CodeCells[uri], #]&
)];
KeyDropFrom[$CodeRange[uri], dropRanges];
AssociateTo[$CodeRange[uri], newRules // KeyTake[addRanges]];
// AssociateTo[$CodeCells[uri], #]&;
AssociateTo[$CodeRange[uri], range -> syntaxTree];
)]]
} /* Through
/* ((
$CodeRange
// Lookup[uri]
// KeyTake[newRules // Keys]
]
// KeyTake[getCodeRanges[doc, ranges]]
)&)
]
// Replace[err:Except[Association[({_Integer, _Integer} -> KeyValuePattern[{"cst" -> _List, "ast" -> _List}])...]] :> (
LogError[{"rangeToSyntaxTree: ", err}]; {}
Expand Down

0 comments on commit b1aaaa8

Please sign in to comment.