本文整理汇总了C#中Microsoft.CodeAnalysis.Document.GetLanguageService方法的典型用法代码示例。如果您正苦于以下问题:C# Document.GetLanguageService方法的具体用法?C# Document.GetLanguageService怎么用?C# Document.GetLanguageService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Document
的用法示例。
在下文中一共展示了Document.GetLanguageService方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetItemsWorkerAsync
protected override async Task<SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
InvocationExpressionSyntax invocationExpression;
if (!TryGetInvocationExpression(root, position, document.GetLanguageService<ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out invocationExpression))
{
return null;
}
var semanticModel = await document.GetSemanticModelForNodeAsync(invocationExpression, cancellationToken).ConfigureAwait(false);
var within = semanticModel.GetEnclosingNamedTypeOrAssembly(position, cancellationToken);
if (within == null)
{
return null;
}
var symbolDisplayService = document.Project.LanguageServices.GetService<ISymbolDisplayService>();
var methodGroup = semanticModel.GetMemberGroup(invocationExpression.Expression, cancellationToken)
.OfType<IMethodSymbol>()
.FilterToVisibleAndBrowsableSymbols(document.ShouldHideAdvancedMembers(), semanticModel.Compilation)
.Sort(symbolDisplayService, semanticModel, invocationExpression.SpanStart);
var expressionType = semanticModel.GetTypeInfo(invocationExpression.Expression, cancellationToken).Type as INamedTypeSymbol;
var anonymousTypeDisplayService = document.Project.LanguageServices.GetService<IAnonymousTypeDisplayService>();
var documentationCommentFormattingService = document.Project.LanguageServices.GetService<IDocumentationCommentFormattingService>();
var textSpan = SignatureHelpUtilities.GetSignatureHelpSpan(invocationExpression.ArgumentList);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
if (methodGroup.Any())
{
return CreateSignatureHelpItems(
GetMethodGroupItems(invocationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, within, methodGroup, cancellationToken),
textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken));
}
else if (expressionType != null && expressionType.TypeKind == TypeKind.Delegate)
{
return CreateSignatureHelpItems(
GetDelegateInvokeItems(invocationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, within, expressionType, cancellationToken),
textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken));
}
else
{
return null;
}
}
示例2: GetItemsWorkerAsync
protected override async Task<SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
if (!TryGetInvocationExpression(root, position, document.GetLanguageService<ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out var invocationExpression))
{
return null;
}
var semanticModel = await document.GetSemanticModelForNodeAsync(invocationExpression, cancellationToken).ConfigureAwait(false);
var within = semanticModel.GetEnclosingNamedTypeOrAssembly(position, cancellationToken);
if (within == null)
{
return null;
}
// get the regular signature help items
var symbolDisplayService = document.Project.LanguageServices.GetService<ISymbolDisplayService>();
var methodGroup = semanticModel.GetMemberGroup(invocationExpression.Expression, cancellationToken)
.OfType<IMethodSymbol>()
.ToImmutableArray()
.FilterToVisibleAndBrowsableSymbols(document.ShouldHideAdvancedMembers(), semanticModel.Compilation);
// try to bind to the actual method
var symbolInfo = semanticModel.GetSymbolInfo(invocationExpression, cancellationToken);
var matchedMethodSymbol = symbolInfo.Symbol as IMethodSymbol;
// if the symbol could be bound, replace that item in the symbol list
if (matchedMethodSymbol != null && matchedMethodSymbol.IsGenericMethod)
{
methodGroup = methodGroup.SelectAsArray(m => matchedMethodSymbol.OriginalDefinition == m ? matchedMethodSymbol : m);
}
methodGroup = methodGroup.Sort(
symbolDisplayService, semanticModel, invocationExpression.SpanStart);
var expressionType = semanticModel.GetTypeInfo(invocationExpression.Expression, cancellationToken).Type as INamedTypeSymbol;
var anonymousTypeDisplayService = document.Project.LanguageServices.GetService<IAnonymousTypeDisplayService>();
var documentationCommentFormattingService = document.Project.LanguageServices.GetService<IDocumentationCommentFormattingService>();
var textSpan = SignatureHelpUtilities.GetSignatureHelpSpan(invocationExpression.ArgumentList);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
if (methodGroup.Any())
{
return CreateSignatureHelpItems(
GetMethodGroupItems(invocationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, within, methodGroup, cancellationToken),
textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken));
}
else if (expressionType != null && expressionType.TypeKind == TypeKind.Delegate)
{
return CreateSignatureHelpItems(
GetDelegateInvokeItems(invocationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, within, expressionType, cancellationToken),
textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken));
}
else
{
return null;
}
}
示例3: AddSourceToAsync
public async Task<Document> AddSourceToAsync(Document document, ISymbol symbol, CancellationToken cancellationToken)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
var newSemanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var rootNamespace = newSemanticModel.GetEnclosingNamespace(0, cancellationToken);
// Add the interface of the symbol to the top of the root namespace
document = await CodeGenerator.AddNamespaceOrTypeDeclarationAsync(
document.Project.Solution,
rootNamespace,
CreateCodeGenerationSymbol(document, symbol),
CreateCodeGenerationOptions(newSemanticModel.SyntaxTree.GetLocation(new TextSpan()), symbol),
cancellationToken).ConfigureAwait(false);
var docCommentFormattingService = document.GetLanguageService<IDocumentationCommentFormattingService>();
var docWithDocComments = await ConvertDocCommentsToRegularComments(document, docCommentFormattingService, cancellationToken).ConfigureAwait(false);
var docWithAssemblyInfo = await AddAssemblyInfoRegionAsync(docWithDocComments, symbol.GetOriginalUnreducedDefinition(), cancellationToken).ConfigureAwait(false);
var node = await docWithAssemblyInfo.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var formattedDoc = await Formatter.FormatAsync(
docWithAssemblyInfo, SpecializedCollections.SingletonEnumerable(node.FullSpan), options: null, rules: GetFormattingRules(docWithAssemblyInfo), cancellationToken: cancellationToken).ConfigureAwait(false);
var reducers = this.GetReducers();
return await Simplifier.ReduceAsync(formattedDoc, reducers, null, cancellationToken).ConfigureAwait(false);
}
示例4: CreateCodeGenerationSymbol
private static INamespaceOrTypeSymbol CreateCodeGenerationSymbol(Document document, ISymbol symbol)
{
symbol = symbol.GetOriginalUnreducedDefinition();
var topLevelNamespaceSymbol = symbol.ContainingNamespace;
var topLevelNamedType = MetadataAsSourceHelpers.GetTopLevelContainingNamedType(symbol);
var canImplementImplicitly = document.GetLanguageService<ISemanticFactsService>().SupportsImplicitInterfaceImplementation;
var docCommentFormattingService = document.GetLanguageService<IDocumentationCommentFormattingService>();
INamespaceOrTypeSymbol wrappedType = new WrappedNamedTypeSymbol(topLevelNamedType, canImplementImplicitly, docCommentFormattingService);
return topLevelNamespaceSymbol.IsGlobalNamespace
? wrappedType
: CodeGenerationSymbolFactory.CreateNamespaceSymbol(
topLevelNamespaceSymbol.ToDisplayString(SymbolDisplayFormats.NameFormat),
null,
new[] { wrappedType });
}
示例5: GetHelpTermAsync
public override async Task<string> GetHelpTermAsync(Document document, TextSpan span, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
// For now, find the token under the start of the selection.
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var token = syntaxTree.GetTouchingToken(span.Start, cancellationToken, findInsideTrivia: true);
if (IsValid(token, span))
{
var semanticModel = await document.GetSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
var result = TryGetText(token, semanticModel, document, cancellationToken, syntaxFacts);
if (string.IsNullOrEmpty(result))
{
var previousToken = token.GetPreviousToken();
if (IsValid(previousToken, span))
{
result = TryGetText(previousToken, semanticModel, document, cancellationToken, syntaxFacts);
}
}
return result;
}
var trivia = root.FindTrivia(span.Start, findInsideTrivia: true);
if (trivia.Span.IntersectsWith(span) && trivia.Kind() == SyntaxKind.PreprocessingMessageTrivia &&
trivia.Token.GetAncestor<RegionDirectiveTriviaSyntax>() != null)
{
return "#region";
}
if (trivia.IsRegularOrDocComment())
{
// just find the first "word" that intersects with our position
var text = await syntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);
int start = span.Start;
int end = span.Start;
while (start > 0 && syntaxFacts.IsIdentifierPartCharacter(text[start - 1]))
{
start--;
}
while (end < text.Length - 1 && syntaxFacts.IsIdentifierPartCharacter(text[end]))
{
end++;
}
return text.GetSubText(TextSpan.FromBounds(start, end)).ToString();
}
return string.Empty;
}
示例6: GetItemsWorkerAsync
protected override async Task<SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
ConstructorInitializerSyntax constructorInitializer;
if (!TryGetConstructorInitializer(root, position, document.GetLanguageService<ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out constructorInitializer))
{
return null;
}
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var within = semanticModel.GetEnclosingNamedType(position, cancellationToken);
if (within == null)
{
return null;
}
if (within.TypeKind != TypeKind.Struct && within.TypeKind != TypeKind.Class)
{
return null;
}
var type = constructorInitializer.Kind() == SyntaxKind.BaseConstructorInitializer
? within.BaseType
: within;
if (type == null)
{
return null;
}
var symbolDisplayService = document.Project.LanguageServices.GetService<ISymbolDisplayService>();
var accessibleConstructors = type.InstanceConstructors
.Where(c => c.IsAccessibleWithin(within))
.Where(c => c.IsEditorBrowsable(document.ShouldHideAdvancedMembers(), semanticModel.Compilation))
.Sort(symbolDisplayService, semanticModel, constructorInitializer.SpanStart);
if (!accessibleConstructors.Any())
{
return null;
}
var anonymousTypeDisplayService = document.Project.LanguageServices.GetService<IAnonymousTypeDisplayService>();
var documentationCommentFormattingService = document.Project.LanguageServices.GetService<IDocumentationCommentFormattingService>();
var textSpan = SignatureHelpUtilities.GetSignatureHelpSpan(constructorInitializer.ArgumentList);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
return CreateSignatureHelpItems(accessibleConstructors.Select(c =>
Convert(c, constructorInitializer.ArgumentList.OpenParenToken, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, cancellationToken)),
textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken));
}
示例7: GetCompletionListProviders
private static IEnumerable<CompletionListProvider> GetCompletionListProviders(Document document)
{
var languageService = document.GetLanguageService<ICompletionService>();
IEnumerable<CompletionListProvider> providers = null;
if (languageService != null)
{
providers = InnerCompletionService.GetDefaultCompletionListProviders(document);
if (_additionalProviders != null)
{
providers = providers.Concat(_additionalProviders);
}
}
return providers;
}
示例8: GetBuilderAsync
protected override async Task<CompletionItem> GetBuilderAsync(Document document, int position, CompletionTriggerInfo triggerInfo, CancellationToken cancellationToken = default(CancellationToken))
{
if (triggerInfo.TriggerReason == CompletionTriggerReason.TypeCharCommand)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
if (triggerInfo.IsDebugger)
{
// Aggressive Intellisense in the debugger: always show the builder
return CreateEmptyBuilder(text, position);
}
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var token = tree
.FindTokenOnLeftOfPosition(position, cancellationToken)
.GetPreviousTokenIfTouchingWord(position);
if (token.Kind() == SyntaxKind.None)
{
return null;
}
var semanticModel = await document.GetSemanticModelForNodeAsync(token.Parent, cancellationToken).ConfigureAwait(false);
var typeInferrer = document.GetLanguageService<ITypeInferenceService>();
if (IsLambdaExpression(semanticModel, position, token, typeInferrer, cancellationToken))
{
return CreateBuilder(text, position, CSharpFeaturesResources.LambdaExpression, CSharpFeaturesResources.AutoselectDisabledDueToPotentialLambdaDeclaration);
}
else if (IsAnonymousObjectCreation(token))
{
return CreateBuilder(text, position, CSharpFeaturesResources.MemberName, CSharpFeaturesResources.AutoselectDisabledDueToPossibleExplicitlyNamesAnonTypeMemCreation);
}
else if (token.IsPreProcessorExpressionContext())
{
return CreateEmptyBuilder(text, position);
}
else if (IsImplicitArrayCreation(semanticModel, token, position, typeInferrer, cancellationToken))
{
return CreateBuilder(text, position, CSharpFeaturesResources.ImplicitArrayCreation, CSharpFeaturesResources.AutoselectDisabledDueToPotentialImplicitArray);
}
else if (token.IsKindOrHasMatchingText(SyntaxKind.FromKeyword) || token.IsKindOrHasMatchingText(SyntaxKind.JoinKeyword))
{
return CreateBuilder(text, position, CSharpFeaturesResources.RangeVariable, CSharpFeaturesResources.AutoselectDisabledDueToPotentialRangeVariableDecl);
}
}
return null;
}
示例9: GetSuggestionModeItemAsync
protected override async Task<CompletionItem> GetSuggestionModeItemAsync(Document document, int position, TextSpan itemSpan, CompletionTrigger trigger, CancellationToken cancellationToken = default(CancellationToken))
{
if (trigger.Kind == CompletionTriggerKind.Insertion)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var token = tree
.FindTokenOnLeftOfPosition(position, cancellationToken)
.GetPreviousTokenIfTouchingWord(position);
if (token.Kind() == SyntaxKind.None)
{
return null;
}
var semanticModel = await document.GetSemanticModelForNodeAsync(token.Parent, cancellationToken).ConfigureAwait(false);
var typeInferrer = document.GetLanguageService<ITypeInferenceService>();
if (IsLambdaExpression(semanticModel, position, token, typeInferrer, cancellationToken))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.LambdaExpression, itemSpan, CSharpFeaturesResources.AutoselectDisabledDueToPotentialLambdaDeclaration);
}
else if (IsAnonymousObjectCreation(token) || IsPossibleTupleExpression(token))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.MemberName, itemSpan, CSharpFeaturesResources.AutoselectDisabledDueToPossibleExplicitlyNamesAnonTypeMemCreation);
}
else if (token.IsPreProcessorExpressionContext())
{
return CreateEmptySuggestionModeItem(itemSpan);
}
else if (IsImplicitArrayCreation(semanticModel, token, position, typeInferrer, cancellationToken))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.ImplicitArrayCreation, itemSpan, CSharpFeaturesResources.AutoselectDisabledDueToPotentialImplicitArray);
}
else if (token.IsKindOrHasMatchingText(SyntaxKind.FromKeyword) || token.IsKindOrHasMatchingText(SyntaxKind.JoinKeyword))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.RangeVariable, itemSpan, CSharpFeaturesResources.AutoselectDisabledDueToPotentialRangeVariableDecl);
}
else if (tree.IsNamespaceDeclarationNameContext(position, cancellationToken))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.NamespaceName, itemSpan, CSharpFeaturesResources.AutoselectDisabledDueToNamespaceDeclaration);
}
}
return null;
}
示例10: GetUpdatedDocumentAsync
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
{
//// We are going to add a call Dispose on fields:
////
//// public void Dispose()
//// {
//// A.Dispose();
//// ...
//// }
var syntaxNode = nodeToFix as VariableDeclaratorSyntax;
if (syntaxNode == null)
{
return Task.FromResult(document);
}
// find a Dispose method
var member = syntaxNode.FirstAncestorOrSelf<ClassDeclarationSyntax>()
.DescendantNodes().OfType<MethodDeclarationSyntax>()
.Where(n => n.Identifier.ValueText == CA2213DiagnosticAnalyzer.Dispose).FirstOrDefault();
if (member == null)
{
return Task.FromResult(document);
}
var factory = document.GetLanguageService<SyntaxGenerator>();
var symbol = model.GetDeclaredSymbol(syntaxNode);
// handle a case where a local in the Dipose method with the same name by generating this (or ClassName) and simplifying it
var path = symbol.IsStatic
? factory.IdentifierName(symbol.ContainingType.MetadataName)
: factory.ThisExpression();
var statement =
factory.ExpressionStatement(
factory.InvocationExpression(
factory.MemberAccessExpression(
factory.MemberAccessExpression(path, factory.IdentifierName(symbol.Name)).WithAdditionalAnnotations(Simplification.Simplifier.Annotation),
factory.IdentifierName(CA2213DiagnosticAnalyzer.Dispose))));
var newMember = member.AddBodyStatements((StatementSyntax)statement).WithAdditionalAnnotations(Formatter.Annotation);
return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(member, newMember)));
}
示例11: GetItemsWorkerAsync
protected override async Task<SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
AttributeSyntax attribute;
if (!TryGetAttributeExpression(root, position, document.GetLanguageService<ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out attribute))
{
return null;
}
var semanticModel = await document.GetSemanticModelForNodeAsync(attribute, cancellationToken).ConfigureAwait(false);
var attributeType = semanticModel.GetTypeInfo(attribute, cancellationToken).Type as INamedTypeSymbol;
if (attributeType == null)
{
return null;
}
var within = semanticModel.GetEnclosingNamedTypeOrAssembly(position, cancellationToken);
if (within == null)
{
return null;
}
var symbolDisplayService = document.Project.LanguageServices.GetService<ISymbolDisplayService>();
var accessibleConstructors = attributeType.InstanceConstructors
.WhereAsArray(c => c.IsAccessibleWithin(within))
.FilterToVisibleAndBrowsableSymbols(document.ShouldHideAdvancedMembers(), semanticModel.Compilation)
.Sort(symbolDisplayService, semanticModel, attribute.SpanStart);
if (!accessibleConstructors.Any())
{
return null;
}
var anonymousTypeDisplayService = document.Project.LanguageServices.GetService<IAnonymousTypeDisplayService>();
var documentationCommentFormatter = document.Project.LanguageServices.GetService<IDocumentationCommentFormattingService>();
var textSpan = SignatureHelpUtilities.GetSignatureHelpSpan(attribute.ArgumentList);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
return CreateSignatureHelpItems(accessibleConstructors.Select(c =>
Convert(c, within, attribute, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormatter, cancellationToken)).ToList(),
textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken));
}
示例12: GetDesiredIndentation
public IndentationResult? GetDesiredIndentation(Document document, int lineNumber, CancellationToken cancellationToken)
{
var root = document.GetSyntaxRootSynchronously(cancellationToken);
var sourceText = root.SyntaxTree.GetText(cancellationToken);
var documentOptions = document.GetOptionsAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var lineToBeIndented = sourceText.Lines[lineNumber];
var formattingRules = GetFormattingRules(document, lineToBeIndented.Start);
// enter on a token case.
if (ShouldUseSmartTokenFormatterInsteadOfIndenter(formattingRules, root, lineToBeIndented, documentOptions, cancellationToken))
{
return null;
}
var indenter = GetIndenter(
document.GetLanguageService<ISyntaxFactsService>(),
root.SyntaxTree, lineToBeIndented, formattingRules,
documentOptions, cancellationToken);
return indenter.GetDesiredIndentation();
}
示例13: GetIssueAsync
protected override async Task<CodeIssue> GetIssueAsync(
Document document,
SyntaxNode node,
CancellationToken cancellationToken)
{
if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles)
{
return null;
}
var service = document.GetLanguageService<IImplementAbstractClassService>();
var result = await service.ImplementAbstractClassAsync(document, node, cancellationToken).ConfigureAwait(false);
var changes = await result.GetTextChangesAsync(document, cancellationToken).ConfigureAwait(false);
if (!changes.Any())
{
return null;
}
return new CodeIssue(
CodeIssueKind.Error,
node.Span,
SpecializedCollections.SingletonEnumerable(new CodeAction(result)));
}
示例14: GenerateProperty
protected IPropertySymbol GenerateProperty(string propertyName, string fieldName, Accessibility accessibility, IFieldSymbol field, INamedTypeSymbol containingSymbol, SyntaxAnnotation annotation, Document document, CancellationToken cancellationToken)
{
var factory = document.GetLanguageService<SyntaxGenerator>();
var propertySymbol = annotation.AddAnnotationToSymbol(CodeGenerationSymbolFactory.CreatePropertySymbol(containingType: containingSymbol,
attributes: SpecializedCollections.EmptyList<AttributeData>(),
accessibility: ComputeAccessibility(accessibility, field.Type),
modifiers: new DeclarationModifiers().WithIsStatic (field.IsStatic).WithIsReadOnly (field.IsReadOnly).WithIsUnsafe (field.IsUnsafe()),
type: field.Type,
explicitInterfaceSymbol: null,
name: propertyName,
parameters: SpecializedCollections.EmptyList<IParameterSymbol>(),
getMethod: CreateGet(fieldName, field, factory),
setMethod: field.IsReadOnly || field.IsConst ? null : CreateSet(fieldName, field, factory)));
return Simplifier.Annotation.AddAnnotationToSymbol(
Formatter.Annotation.AddAnnotationToSymbol(propertySymbol));
}
示例15: GetSuggestionModeItemAsync
protected override async Task<CompletionItem> GetSuggestionModeItemAsync(
Document document, int position, TextSpan itemSpan, CompletionTrigger trigger, CancellationToken cancellationToken = default(CancellationToken))
{
if (trigger.Kind == CompletionTriggerKind.Insertion)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var token = tree
.FindTokenOnLeftOfPosition(position, cancellationToken)
.GetPreviousTokenIfTouchingWord(position);
if (token.Kind() == SyntaxKind.None)
{
return null;
}
var semanticModel = await document.GetSemanticModelForNodeAsync(token.Parent, cancellationToken).ConfigureAwait(false);
var typeInferrer = document.GetLanguageService<ITypeInferenceService>();
if (IsLambdaExpression(semanticModel, position, token, typeInferrer, cancellationToken))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.lambda_expression, CSharpFeaturesResources.Autoselect_disabled_due_to_potential_lambda_declaration);
}
else if (IsAnonymousObjectCreation(token))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.member_name, CSharpFeaturesResources.Autoselect_disabled_due_to_possible_explicitly_named_anonymous_type_member_creation);
}
else if (token.IsPreProcessorExpressionContext())
{
return CreateEmptySuggestionModeItem();
}
else if (IsImplicitArrayCreation(semanticModel, token, position, typeInferrer, cancellationToken))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.implicit_array_creation, CSharpFeaturesResources.Autoselect_disabled_due_to_potential_implicit_array_creation);
}
else if (token.IsKindOrHasMatchingText(SyntaxKind.FromKeyword) || token.IsKindOrHasMatchingText(SyntaxKind.JoinKeyword))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.range_variable, CSharpFeaturesResources.Autoselect_disabled_due_to_potential_range_variable_declaration);
}
else if (tree.IsNamespaceDeclarationNameContext(position, cancellationToken))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.namespace_name, CSharpFeaturesResources.Autoselect_disabled_due_to_namespace_declaration);
}
else if (tree.IsPartialTypeDeclarationNameContext(position, cancellationToken, out var typeDeclaration))
{
switch (typeDeclaration.Keyword.Kind())
{
case SyntaxKind.ClassKeyword:
return CreateSuggestionModeItem(CSharpFeaturesResources.class_name, CSharpFeaturesResources.Autoselect_disabled_due_to_type_declaration);
case SyntaxKind.StructKeyword:
return CreateSuggestionModeItem(CSharpFeaturesResources.struct_name, CSharpFeaturesResources.Autoselect_disabled_due_to_type_declaration);
case SyntaxKind.InterfaceKeyword:
return CreateSuggestionModeItem(CSharpFeaturesResources.interface_name, CSharpFeaturesResources.Autoselect_disabled_due_to_type_declaration);
}
}
else if (tree.IsPossibleDeconstructionDesignation(position, cancellationToken))
{
return CreateSuggestionModeItem(CSharpFeaturesResources.designation_name,
CSharpFeaturesResources.Autoselect_disabled_due_to_possible_deconstruction_declaration);
}
}
return null;
}