Joachim, Louis,
Both of these are great approaches...or really the combination of both these ideas would probably be the way to go.
(I'll call the "pivot" the place in the workspace where the parser should start)
If Louis's idea worked out, the best pivot would be right before the "| temp1 temp2 |" declarations. This would provide the most
information about variables and what they are, what gets assigned to them, what messages they understand, what objects are returned by these messages
and the whole chain reaction that follows until you arrive at the current cursor position with hopefully a wealth of information.
The issue with this approach is that the way folks use workspaces can easily get one into situations where a valid parse tree is created, but is semantically incorrect. And because
of this, it's not easily detectable.
Example:
| temp1 temp2 |
temp1 := OrderedCollection new. "No Problem"
temp2 := OrderedCollection new.
temp1 asSortedCollection "Line 5: No Statement terminator"
"Snippet for a separate task"
temp2 <Request Code Assist> "Line 8"
The workspace writer probably intends this to be 2 separate snippets of code that they evaluate separately as needed. It's probably an unreasonable assumption that everyone statement terminates all of their snippets.
One could make it a requirement for Code Assist to work, but I don't like that kind of stuff if I can help it.
On Line 5 the parser moves forward and sees "temp1 asSortedCollection temp2" which is actually valid (even though temp2 is probably not an implemented message). So this creates a valid parse tree but we know it's incorrect
based on our "very loosely" defined formatting rules of separating the 2 snippets with a carriage return (in this case). There are many variations that can lead to this kind of situation.
Without building in semantic rule-sets (which only have a probability of being correct), this isn't a good pivot to pick to guarantee success. As Joachim was suggesting, we could put the pivot close to the cursor point and backup.
Unfortunately, this can fail too with variations of the example above which I won't enumerate on at the moment.
The question may just come down to what's good enough. I don't mind this approach, but there are 2 caveats I would add. The first is I do mind showing incorrect results to the user, and if I can't detect if it's incorrect then that is a problem.
And the second is pretty simple...which is, while we are doing this...we might as well do it well:)
So I'm thinking out loud a little, but I'm still favoring a combination approach of what has been suggested, but trying to put together some REALLY GOOD snippet detection so we can get past this problem and make code assist more useful for workspaces.
Thank you for the ideas...very good thoughts.
-- Seth