本文整理汇总了C#中SyntaxContext类的典型用法代码示例。如果您正苦于以下问题:C# SyntaxContext类的具体用法?C# SyntaxContext怎么用?C# SyntaxContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SyntaxContext类属于命名空间,在下文中一共展示了SyntaxContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetItemsWorkerAsync
protected override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var position = completionContext.Position;
var document = completionContext.Document;
var syntaxTree = ctx.SyntaxTree;
if (syntaxTree.IsInNonUserCode(position, cancellationToken) ||
syntaxTree.IsPreProcessorDirectiveContext(position, cancellationToken))
return Task.FromResult (Enumerable.Empty<CompletionData> ());
if (!syntaxTree.IsRightOfDotOrArrowOrColonColon(position, cancellationToken))
return Task.FromResult (Enumerable.Empty<CompletionData> ());
var ma = ctx.LeftToken.Parent as MemberAccessExpressionSyntax;
if (ma == null)
return Task.FromResult (Enumerable.Empty<CompletionData> ());
var model = ctx.CSharpSyntaxContext.SemanticModel;
var symbolInfo = model.GetSymbolInfo (ma.Expression);
if (symbolInfo.Symbol == null || symbolInfo.Symbol.Kind != SymbolKind.Parameter)
return Task.FromResult (Enumerable.Empty<CompletionData> ());
var list = new List<CompletionData> ();
var within = model.GetEnclosingNamedTypeOrAssembly(position, cancellationToken);
var addedSymbols = new HashSet<string> ();
foreach (var ano in ma.AncestorsAndSelf ().OfType<AnonymousMethodExpressionSyntax> ()) {
Analyze (engine, model, ma.Expression, within, list, ano.ParameterList, symbolInfo.Symbol, addedSymbols, cancellationToken);
}
foreach (var ano in ma.AncestorsAndSelf ().OfType<ParenthesizedLambdaExpressionSyntax> ()) {
Analyze (engine, model, ma.Expression, within, list, ano.ParameterList, symbolInfo.Symbol, addedSymbols, cancellationToken);
}
return Task.FromResult ((IEnumerable<CompletionData>)list);
}
示例2: GetItemsWorkerAsync
protected async override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var model = ctx.SemanticModel;
var tree = ctx.SyntaxTree;
if (tree.IsInNonUserCode (completionContext.Position, cancellationToken))
return Enumerable.Empty<CompletionData> ();
var token = tree.FindTokenOnLeftOfPosition (completionContext.Position, cancellationToken);
if (token.IsMandatoryNamedParameterPosition ())
return Enumerable.Empty<CompletionData> ();
var result = new List<CompletionData> ();
// check if it's the first parameter and set autoselect == false if a parameterless version exists.
if (token.IsKind (SyntaxKind.OpenParenToken)) {
var parent = token.Parent?.Parent;
if (parent == null)
return Enumerable.Empty<CompletionData> ();
var symbolInfo = model.GetSymbolInfo (parent);
foreach (var symbol in new [] { symbolInfo.Symbol }.Concat (symbolInfo.CandidateSymbols)) {
if (symbol != null && symbol.IsKind (SymbolKind.Method)) {
if (symbol.GetParameters ().Length == 0) {
completionResult.AutoSelect = false;
break;
}
}
}
}
foreach (var _type in ctx.InferredTypes) {
var type = _type;
if (type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) {
type = type.GetTypeArguments ().FirstOrDefault ();
if (type == null)
continue;
}
if (type.TypeKind != TypeKind.Enum)
continue;
if (!type.IsEditorBrowsable ())
continue;
// Does type have any aliases?
ISymbol alias = await type.FindApplicableAlias (completionContext.Position, model, cancellationToken).ConfigureAwait (false);
var displayString = RoslynCompletionData.SafeMinimalDisplayString (type, model, completionContext.Position, SymbolDisplayFormat.CSharpErrorMessageFormat);
if (string.IsNullOrEmpty (completionResult.DefaultCompletionString)) {
completionResult.DefaultCompletionString = displayString;
completionResult.AutoCompleteEmptyMatch = true;
}
if (!IsReachable (model, type, token.Parent))
result.Add (engine.Factory.CreateSymbolCompletionData (this, type, displayString));
foreach (IFieldSymbol field in type.GetMembers ().OfType<IFieldSymbol> ()) {
if (field.DeclaredAccessibility == Accessibility.Public && (field.IsConst || field.IsStatic)) {
result.Add (engine.Factory.CreateEnumMemberCompletionData (this, alias, field));
}
}
}
return result;
}
示例3: GetItemsWorkerAsync
protected override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult result, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var document = completionContext.Document;
var position = completionContext.Position;
var tree = ctx.SyntaxTree;
var model = ctx.SemanticModel;
if (tree.IsInNonUserCode (position, cancellationToken))
return Task.FromResult (Enumerable.Empty<CompletionData> ());
if (!ctx.CSharpSyntaxContext.IsAnyExpressionContext)
return Task.FromResult (Enumerable.Empty<CompletionData> ());
var enclosingType = model.GetEnclosingNamedType (position, cancellationToken);
var memberMethods = enclosingType.GetMembers ().OfType<IMethodSymbol> ().Where (m => m.MethodKind == MethodKind.Ordinary).ToArray ();
var list = new List<CompletionData> ();
foreach (var type in ctx.InferredTypes) {
if (type.TypeKind != TypeKind.Delegate)
continue;
AddCompatibleMethods (engine, list, type, memberMethods, cancellationToken);
string delegateName = null;
if (ctx.TargetToken.IsKind (SyntaxKind.PlusEqualsToken)) {
delegateName = GuessEventHandlerBaseName (ctx.LeftToken.Parent, ctx.ContainingTypeDeclaration);
}
AddDelegateHandlers (list, ctx.TargetToken.Parent, model, engine, result, type, position, delegateName, cancellationToken);
}
if (list.Count > 0) {
result.AutoSelect = false;
}
return Task.FromResult ((IEnumerable<CompletionData>)list);
}
示例4: GetItemsWorkerAsync
protected async override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var document = completionContext.Document;
var position = completionContext.Position;
var tree = ctx.SyntaxTree;
//DeclarationModifiers modifiers;
SyntaxToken token;
var semanticModel = ctx.SemanticModel;
var enclosingSymbol = semanticModel.GetEnclosingSymbol (position, cancellationToken) as INamedTypeSymbol;
// Only inside classes and structs
if (enclosingSymbol == null || !(enclosingSymbol.TypeKind == TypeKind.Struct || enclosingSymbol.TypeKind == TypeKind.Class)) {
return Enumerable.Empty<CompletionData> ();
}
if (!IsPartialCompletionContext (tree, position, cancellationToken/*, out modifiers*/, out token)) {
if (enclosingSymbol != null && (token.IsKind (SyntaxKind.OpenBraceToken) || token.IsKind (SyntaxKind.CloseBraceToken) || token.IsKind (SyntaxKind.SemicolonToken))) {
return CreateCompletionData (engine, semanticModel, position, enclosingSymbol, token, false, cancellationToken);
}
return Enumerable.Empty<CompletionData> ();
}
return CreateCompletionData (engine, semanticModel, position, enclosingSymbol, token, true, cancellationToken);
}
示例5: GetItemsWorkerAsync
protected async override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var position = completionContext.Position;
var document = completionContext.Document;
var span = new TextSpan(position, 0);
var semanticModel = await document.GetCSharpSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
var syntaxTree = semanticModel.SyntaxTree;
// var ctx = await completionContext.GetSyntaxContextAsync (engine.Workspace, cancellationToken).ConfigureAwait (false);
if (syntaxTree.IsInNonUserCode(position, cancellationToken) ||
syntaxTree.IsPreProcessorDirectiveContext(position, cancellationToken))
{
return Enumerable.Empty<CompletionData> ();
}
if (!syntaxTree.IsRightOfDotOrArrowOrColonColon(position, cancellationToken))
{
return Enumerable.Empty<CompletionData> ();
}
var node = syntaxTree.FindTokenOnLeftOfPosition(position, cancellationToken)
.GetPreviousTokenIfTouchingWord(position)
.Parent;
if (node.Kind() == SyntaxKind.ExplicitInterfaceSpecifier)
{
return await GetCompletionsOffOfExplicitInterfaceAsync(
engine, document, semanticModel, position, ((ExplicitInterfaceSpecifierSyntax)node).Name, cancellationToken).ConfigureAwait(false);
}
return Enumerable.Empty<CompletionData> ();
}
示例6: GetItemsWorkerAsync
protected async override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var document = completionContext.Document;
var position = completionContext.Position;
var semanticModel = ctx.SemanticModel;
if (ctx.TargetToken.Parent != null && ctx.TargetToken.Parent.Parent != null &&
ctx.TargetToken.Parent.Parent.IsKind(SyntaxKind.Argument)) {
SourceText text;
if (!completionContext.Document.TryGetText (out text)) {
text = await completionContext.Document.GetTextAsync ();
}
var currentChar = text [completionContext.Position - 1];
if (ctx.TargetToken.Parent == null || !ctx.TargetToken.Parent.IsKind(SyntaxKind.StringLiteralExpression) ||
ctx.TargetToken.Parent.Parent == null || !ctx.TargetToken.Parent.Parent.IsKind(SyntaxKind.Argument) ||
ctx.TargetToken.Parent.Parent.Parent == null || !ctx.TargetToken.Parent.Parent.Parent.IsKind(SyntaxKind.ArgumentList) ||
ctx.TargetToken.Parent.Parent.Parent.Parent == null || !ctx.TargetToken.Parent.Parent.Parent.Parent.IsKind(SyntaxKind.InvocationExpression)) {
return Enumerable.Empty<CompletionData> ();
}
var formatArgument = GetFormatItemNumber(document, position);
var invocationExpression = ctx.TargetToken.Parent.Parent.Parent.Parent as InvocationExpressionSyntax;
return GetFormatCompletionData(engine, semanticModel, invocationExpression, formatArgument, currentChar);
}
return Enumerable.Empty<CompletionData> ();
}
示例7: GetItemsWorkerAsync
protected async override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var document = completionContext.Document;
var position = completionContext.Position;
var tree = ctx.SyntaxTree;
if (tree.IsInNonUserCode(position, cancellationToken))
return Enumerable.Empty<CompletionData> ();
var targetToken = tree.FindTokenOnLeftOfPosition(position, cancellationToken).GetPreviousTokenIfTouchingWord(position);
if (targetToken.IsKind(SyntaxKind.AliasKeyword) && targetToken.Parent.IsKind(SyntaxKind.ExternAliasDirective))
{
var compilation = await document.GetCSharpCompilationAsync(cancellationToken).ConfigureAwait(false);
var aliases = compilation.ExternalReferences.Where(r => r.Properties.Aliases != null).SelectMany(r => r.Properties.Aliases).ToSet();
if (aliases.Any())
{
var root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var usedAliases = root.ChildNodes().OfType<ExternAliasDirectiveSyntax>().Where(e => !e.Identifier.IsMissing).Select(e => e.Identifier.ValueText);
foreach (var used in usedAliases) {
aliases.Remove (used);
}
aliases.Remove(MetadataReferenceProperties.GlobalAlias);
return aliases.Select (e => engine.Factory.CreateGenericData (this, e, GenericDataType.Undefined));
}
}
return Enumerable.Empty<CompletionData> ();
}
示例8: CreateItem
/// <summary>
/// Given a Symbol, creates the completion item for it.
/// </summary>
private CompletionItem CreateItem(
string displayText,
string insertionText,
List<ISymbol> symbols,
SyntaxContext context,
Dictionary<ISymbol, List<ProjectId>> invalidProjectMap,
List<ProjectId> totalProjects,
bool preselect)
{
Contract.ThrowIfNull(symbols);
SupportedPlatformData supportedPlatformData = null;
if (invalidProjectMap != null)
{
List<ProjectId> invalidProjects = null;
foreach (var symbol in symbols)
{
if (invalidProjectMap.TryGetValue(symbol, out invalidProjects))
{
break;
}
}
if (invalidProjects != null)
{
supportedPlatformData = new SupportedPlatformData(invalidProjects, totalProjects, context.Workspace);
}
}
return CreateItem(displayText, insertionText, symbols, context, preselect, supportedPlatformData);
}
示例9: LookupCandidateSymbols
protected override IEnumerable<INamedTypeSymbol> LookupCandidateSymbols(SyntaxContext context, INamedTypeSymbol declaredSymbol, CancellationToken cancellationToken)
{
var candidates = base.LookupCandidateSymbols(context, declaredSymbol, cancellationToken);
// The base class applies a broad filter when finding candidates, but since C# requires
// that all parts have the "partial" modifier, the results can be trimmed further here.
return candidates?.Where(symbol => symbol.DeclaringSyntaxReferences.Any(reference => IsPartialTypeDeclaration(reference.GetSyntax(cancellationToken))));
}
示例10: IsExclusiveAsync
public override async Task<bool> IsExclusiveAsync (CompletionContext completionContext, SyntaxContext syntaxContext, CompletionTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
// We're exclusive if this context could only be an object initializer and not also a
// collection initializer. If we're initializing something that could be initialized as
// an object or as a collection, say we're not exclusive. That way the rest of
// intellisense can be used in the collection intitializer.
//
// Consider this case:
// class c : IEnumerable<int>
// {
// public void Add(int addend) { }
// public int foo;
// }
// void foo()
// {
// var b = new c {|
// }
// There we could initialize b using either an object initializer or a collection
// initializer. Since we don't know which the user will use, we'll be non-exclusive, so
// the other providers can help the user write the collection initializer, if they want
// to.
var document = completionContext.Document;
var position = completionContext.Position;
var tree = await document.GetCSharpSyntaxTreeAsync (cancellationToken).ConfigureAwait (false);
if (tree.IsInNonUserCode (position, cancellationToken)) {
return false;
}
var token = tree.FindTokenOnLeftOfPosition (position, cancellationToken);
token = token.GetPreviousTokenIfTouchingWord (position);
if (token.Parent == null) {
return false;
}
var expression = token.Parent.Parent as ExpressionSyntax;
if (expression == null) {
return false;
}
var semanticModel = await document.GetCSharpSemanticModelForNodeAsync (expression, cancellationToken).ConfigureAwait (false);
var initializedType = semanticModel.GetTypeInfo (expression, cancellationToken).Type;
if (initializedType == null) {
return false;
}
// Non-exclusive if initializedType can be initialized as a collection.
if (initializedType.CanSupportCollectionInitializer ()) {
return false;
}
// By default, only our member names will show up.
return true;
}
示例11: override
protected override (string displayText, string insertionText) GetDisplayAndInsertionText(ISymbol symbol, SyntaxContext context)
{
if (symbol is IAliasSymbol)
{
return (symbol.Name, symbol.Name);
}
return base.GetDisplayAndInsertionText(symbol, context);
}
示例12: GetDisplayAndInsertionText
protected override ValueTuple<string, string> GetDisplayAndInsertionText(ISymbol symbol, SyntaxContext context)
{
if (symbol is IAliasSymbol)
{
return ValueTuple.Create(symbol.Name, symbol.Name);
}
return base.GetDisplayAndInsertionText(symbol, context);
}
示例13: GetItemsWorkerAsync
protected async override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var document = completionContext.Document;
var position = completionContext.Position;
return await document.GetUnionResultsFromDocumentAndLinks(
UnionCompletionItemComparer.Instance,
async (doc, ct) => await GetSpeculativeTCompletions(engine, doc, position, ct).ConfigureAwait(false),
cancellationToken).ConfigureAwait(false);
}
示例14: GetItemsWorkerAsync
protected async override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var document = completionContext.Document;
var position = completionContext.Position;
var syntaxTree = await document.GetCSharpSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
if (syntaxTree.IsInNonUserCode(position, cancellationToken) ||
syntaxTree.IsRightOfDotOrArrowOrColonColon(position, cancellationToken) ||
syntaxTree.GetContainingTypeOrEnumDeclaration(position, cancellationToken) is EnumDeclarationSyntax)
{
return Enumerable.Empty<CompletionData>();
}
// var span = new TextSpan(position, 0);
// var semanticModel = await document.GetCSharpSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
if (syntaxTree.IsPreProcessorDirectiveContext(position, cancellationToken))
{
var directive = syntaxTree.GetRoot(cancellationToken).FindTokenOnLeftOfPosition(position, includeDirectives: true).GetAncestor<DirectiveTriviaSyntax>();
if (directive.DirectiveNameToken.IsKind(
SyntaxKind.IfKeyword,
SyntaxKind.RegionKeyword,
SyntaxKind.ElseKeyword,
SyntaxKind.ElifKeyword,
SyntaxKind.ErrorKeyword,
SyntaxKind.LineKeyword,
SyntaxKind.PragmaKeyword,
SyntaxKind.EndIfKeyword,
SyntaxKind.UndefKeyword,
SyntaxKind.EndRegionKeyword,
SyntaxKind.WarningKeyword))
{
return Enumerable.Empty<CompletionData>();
}
return await GetSnippetCompletionItemsAsync(cancellationToken).ConfigureAwait(false);
}
var tokenLeftOfPosition = syntaxTree.FindTokenOnLeftOfPosition (position, cancellationToken);
if (syntaxTree.IsGlobalStatementContext(position, cancellationToken) ||
syntaxTree.IsExpressionContext(position, tokenLeftOfPosition, true, cancellationToken) ||
syntaxTree.IsStatementContext(position, tokenLeftOfPosition, cancellationToken) ||
syntaxTree.IsTypeContext(position, cancellationToken) ||
syntaxTree.IsTypeDeclarationContext(position, tokenLeftOfPosition, cancellationToken) ||
syntaxTree.IsNamespaceContext(position, cancellationToken) ||
syntaxTree.IsMemberDeclarationContext(position, tokenLeftOfPosition, cancellationToken) ||
syntaxTree.IsLabelContext(position, cancellationToken))
{
return await GetSnippetCompletionItemsAsync(cancellationToken).ConfigureAwait(false);
}
return Enumerable.Empty<CompletionData>();
}
示例15: GetItemsWorkerAsync
protected async override Task<IEnumerable<CompletionData>> GetItemsWorkerAsync (CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken)
{
var model = ctx.SemanticModel;
var result = new List<CompletionData> ();
if (ctx.IsPreProcessorExpressionContext) {
var parseOptions = model.SyntaxTree.Options as CSharpParseOptions;
foreach (var define in parseOptions.PreprocessorSymbolNames) {
result.Add(engine.Factory.CreateGenericData (this, define, GenericDataType.PreprocessorSymbol));
}
}
return result;
}