-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed peek definition tokenizing #281
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4be0639
fixed peek definition tokenizing
b5a2fc5
fixed signature help test
9545e3b
added new heuristic for PeekDefinition behaviour
0ed440d
added tests for new heuristic
194b228
merged from dev
bdea4c3
Merge branch 'dev' into task/PeekDefinitionTokenizer
e660c8c
fixed code according to Kevin's CR
5418dc5
fixed failing test due to shared connection
b12ab7d
changed uri for procedure test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -820,6 +820,54 @@ private DefinitionResult QueueTask(TextDocumentPosition textDocumentPosition, Sc | |
return result; | ||
} | ||
|
||
private DefinitionResult GetDefinitionFromTokenList(TextDocumentPosition textDocumentPosition, List<Token> tokenList, | ||
ScriptParseInfo scriptParseInfo, ScriptFile scriptFile, ConnectionInfo connInfo) | ||
{ | ||
|
||
DefinitionResult lastResult = null; | ||
foreach (var token in tokenList) | ||
{ | ||
|
||
// Strip "[" and "]"(if present) from the token text to enable matching with the suggestions. | ||
// The suggestion title does not contain any sql punctuation | ||
string tokenText = TextUtilities.RemoveSquareBracketSyntax(token.Text); | ||
textDocumentPosition.Position.Line = token.StartLocation.LineNumber; | ||
textDocumentPosition.Position.Character = token.StartLocation.ColumnNumber; | ||
if (Monitor.TryEnter(scriptParseInfo.BuildingMetadataLock)) | ||
{ | ||
try | ||
{ | ||
var result = QueueTask(textDocumentPosition, scriptParseInfo, connInfo, scriptFile, tokenText); | ||
lastResult = result; | ||
if (!result.IsErrorResult) | ||
{ | ||
return result; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
// if any exceptions are raised return error result with message | ||
Logger.Write(LogLevel.Error, "Exception in GetDefinition " + ex.ToString()); | ||
return new DefinitionResult | ||
{ | ||
IsErrorResult = true, | ||
Message = SR.PeekDefinitionError(ex.Message), | ||
Locations = null | ||
}; | ||
} | ||
finally | ||
{ | ||
Monitor.Exit(scriptParseInfo.BuildingMetadataLock); | ||
} | ||
} | ||
else | ||
{ | ||
Logger.Write(LogLevel.Error, "Timeout waiting to query metadata from server"); | ||
} | ||
} | ||
return (lastResult != null) ? lastResult : null; | ||
} | ||
|
||
/// <summary> | ||
/// Get definition for a selected sql object using SMO Scripting | ||
/// </summary> | ||
|
@@ -842,109 +890,45 @@ internal DefinitionResult GetDefinition(TextDocumentPosition textDocumentPositio | |
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd put an isconnected check at the top and error out cleanly here. |
||
|
||
// Get token from selected text | ||
var selectedToken = ScriptDocumentInfo.GetPeekDefinitionTokens(scriptParseInfo, textDocumentPosition.Position.Line + 1, textDocumentPosition.Position.Character + 1); | ||
Tuple<Stack<Token>, Queue<Token>> selectedToken = ScriptDocumentInfo.GetPeekDefinitionTokens(scriptParseInfo, | ||
textDocumentPosition.Position.Line + 1, textDocumentPosition.Position.Character + 1); | ||
|
||
if (selectedToken == null) | ||
{ | ||
return null; | ||
} | ||
|
||
DefinitionResult lastResult = null; | ||
|
||
//try children tokens first | ||
foreach (var i in selectedToken.Item1.ToList()) | ||
{ | ||
var token = selectedToken.Item1.Pop(); | ||
// Strip "[" and "]"(if present) from the token text to enable matching with the suggestions. | ||
// The suggestion title does not contain any sql punctuation | ||
string tokenText = TextUtilities.RemoveSquareBracketSyntax(token.Text); | ||
textDocumentPosition.Position.Line = token.StartLocation.LineNumber; | ||
textDocumentPosition.Position.Character = token.StartLocation.ColumnNumber; | ||
if (scriptParseInfo.IsConnected && Monitor.TryEnter(scriptParseInfo.BuildingMetadataLock)) | ||
{ | ||
try | ||
{ | ||
var result = QueueTask(textDocumentPosition, scriptParseInfo, connInfo, scriptFile, tokenText); | ||
if (!result.IsErrorResult) | ||
{ | ||
return result; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
// if any exceptions are raised return error result with message | ||
Logger.Write(LogLevel.Error, "Exception in GetDefinition " + ex.ToString()); | ||
return new DefinitionResult | ||
{ | ||
IsErrorResult = true, | ||
Message = SR.PeekDefinitionError(ex.Message), | ||
Locations = null | ||
}; | ||
} | ||
finally | ||
{ | ||
Monitor.Exit(scriptParseInfo.BuildingMetadataLock); | ||
} | ||
} | ||
else | ||
{ | ||
// User is not connected. | ||
return new DefinitionResult | ||
{ | ||
IsErrorResult = true, | ||
Message = SR.PeekDefinitionNotConnectedError, | ||
Locations = null | ||
}; | ||
} | ||
} | ||
|
||
// then check the parents | ||
foreach (var i in selectedToken.Item2.ToList()) | ||
{ | ||
var token = selectedToken.Item2.Dequeue(); | ||
// Strip "[" and "]"(if present) from the token text to enable matching with the suggestions. | ||
// The suggestion title does not contain any sql punctuation | ||
string tokenText = TextUtilities.RemoveSquareBracketSyntax(token.Text); | ||
textDocumentPosition.Position.Line = token.StartLocation.LineNumber; | ||
textDocumentPosition.Position.Character = token.StartLocation.ColumnNumber; | ||
if (scriptParseInfo.IsConnected && Monitor.TryEnter(scriptParseInfo.BuildingMetadataLock)) | ||
{ | ||
try | ||
{ | ||
var result = QueueTask(textDocumentPosition, scriptParseInfo, connInfo, scriptFile, tokenText); | ||
lastResult = result; | ||
if (!result.IsErrorResult) | ||
{ | ||
return result; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
// if any exceptions are raised return error result with message | ||
Logger.Write(LogLevel.Error, "Exception in GetDefinition " + ex.ToString()); | ||
return new DefinitionResult | ||
{ | ||
IsErrorResult = true, | ||
Message = SR.PeekDefinitionError(ex.Message), | ||
Locations = null | ||
}; | ||
} | ||
finally | ||
{ | ||
Monitor.Exit(scriptParseInfo.BuildingMetadataLock); | ||
} | ||
} | ||
else | ||
{ | ||
// User is not connected. | ||
return new DefinitionResult | ||
{ | ||
IsErrorResult = true, | ||
Message = SR.PeekDefinitionNotConnectedError, | ||
Locations = null | ||
}; | ||
} | ||
if (scriptParseInfo.IsConnected) | ||
{ | ||
//try children tokens first | ||
Stack<Token> childrenTokens = selectedToken.Item1; | ||
List<Token> tokenList = childrenTokens.ToList(); | ||
DefinitionResult childrenResult = GetDefinitionFromTokenList(textDocumentPosition, tokenList, scriptParseInfo, scriptFile, connInfo); | ||
|
||
// if the children peak definition returned null then | ||
// try the parents | ||
if (childrenResult == null || childrenResult.IsErrorResult) | ||
{ | ||
Queue<Token> parentTokens = selectedToken.Item2; | ||
tokenList = parentTokens.ToList(); | ||
DefinitionResult parentResult = GetDefinitionFromTokenList(textDocumentPosition, tokenList, scriptParseInfo, scriptFile, connInfo); | ||
return (parentResult == null) ? null : parentResult; | ||
} | ||
else | ||
{ | ||
return childrenResult; | ||
} | ||
} | ||
else | ||
{ | ||
// User is not connected. | ||
return new DefinitionResult | ||
{ | ||
IsErrorResult = true, | ||
Message = SR.PeekDefinitionNotConnectedError, | ||
Locations = null | ||
}; | ||
} | ||
return (lastResult != null) ? lastResult : null; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -954,7 +938,7 @@ internal DefinitionResult GetDefinition(TextDocumentPosition textDocumentPositio | |
/// <param name="startLine"></param> | ||
/// <param name="startColumn"></param> | ||
/// <returns> token index</returns> | ||
private int FindTokenWrapper(ScriptParseInfo scriptParseInfo, int startLine, int startColumn) | ||
private int FindTokenWithCorrectOffset(ScriptParseInfo scriptParseInfo, int startLine, int startColumn) | ||
{ | ||
var tokenIndex = scriptParseInfo.ParseResult.Script.TokenManager.FindToken(startLine, startColumn); | ||
var end = scriptParseInfo.ParseResult.Script.TokenManager.GetToken(tokenIndex).EndLocation; | ||
|
@@ -981,7 +965,7 @@ private string GetSchemaName(ScriptParseInfo scriptParseInfo, Position position, | |
// Get schema name | ||
if (scriptParseInfo != null && scriptParseInfo.ParseResult != null && scriptParseInfo.ParseResult.Script != null && scriptParseInfo.ParseResult.Script.Tokens != null) | ||
{ | ||
var tokenIndex = FindTokenWrapper(scriptParseInfo, startLine, startColumn); | ||
var tokenIndex = FindTokenWithCorrectOffset(scriptParseInfo, startLine, startColumn); | ||
var prevTokenIndex = scriptParseInfo.ParseResult.Script.TokenManager.GetPreviousSignificantTokenIndex(tokenIndex); | ||
var prevTokenText = scriptParseInfo.ParseResult.Script.TokenManager.GetText(prevTokenIndex); | ||
if (prevTokenText != null && prevTokenText.Equals(".")) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: minimize whitespace lines