public void TestCombineEffectiveAccessibility() { // If enclosing is private, declared does not matter Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.NotApplicable, Accessibility.Private)); Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Private, Accessibility.Private)); Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Internal, Accessibility.Private)); Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Protected, Accessibility.Private)); Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Public, Accessibility.Private)); // Test enclosing is ProtectedAndInternal Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Private, Accessibility.ProtectedAndInternal)); Assert.Equal(Accessibility.ProtectedAndInternal, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Public, Accessibility.ProtectedAndInternal)); // Test enclosing is Protected Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Private, Accessibility.Protected)); Assert.Equal(Accessibility.ProtectedAndInternal, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Internal, Accessibility.Protected)); Assert.Equal(Accessibility.Protected, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Public, Accessibility.Protected)); // Test enclosing is Internal Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Private, Accessibility.Internal)); Assert.Equal(Accessibility.ProtectedAndInternal, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Protected, Accessibility.Internal)); Assert.Equal(Accessibility.Internal, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Public, Accessibility.Internal)); // Test enclosing is ProtectedOrInternal Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Private, Accessibility.ProtectedOrInternal)); Assert.Equal(Accessibility.ProtectedOrInternal, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Public, Accessibility.ProtectedOrInternal)); // Test enclosing is Public Assert.Equal(Accessibility.Private, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Private, Accessibility.Public)); Assert.Equal(Accessibility.Public, AccessLevelHelper.CombineEffectiveAccessibility(Accessibility.Public, Accessibility.Public)); }
private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context) { var typeDeclaration = (TypeDeclarationSyntax)context.Node; var previousFieldReadonly = true; var previousAccessLevel = AccessLevel.NotSpecified; var previousMemberStaticOrConst = true; foreach (var member in typeDeclaration.Members) { var field = member as FieldDeclarationSyntax; if (field == null) { continue; } var currentFieldReadonly = field.Modifiers.Any(SyntaxKind.ReadOnlyKeyword); var currentAccessLevel = AccessLevelHelper.GetAccessLevel(field.Modifiers); currentAccessLevel = currentAccessLevel == AccessLevel.NotSpecified ? AccessLevel.Private : currentAccessLevel; var currentMemberStaticOrConst = field.Modifiers.Any(SyntaxKind.StaticKeyword) || field.Modifiers.Any(SyntaxKind.ConstKeyword); if (currentAccessLevel == previousAccessLevel && !currentMemberStaticOrConst && !previousMemberStaticOrConst && currentFieldReadonly && !previousFieldReadonly) { context.ReportDiagnostic(Diagnostic.Create(Descriptor, NamedTypeHelpers.GetNameOrIdentifierLocation(field), AccessLevelHelper.GetName(currentAccessLevel))); } previousFieldReadonly = currentFieldReadonly; previousAccessLevel = currentAccessLevel; previousMemberStaticOrConst = currentMemberStaticOrConst; } }
private void OnPart(PacketInvocationData pid) { if (pid.Content.Length == 0) { return; } var channelName = pid.Content; if (channelName.Contains(' ')) { channelName = channelName.Substring(0, channelName.IndexOf(' ')); } var channel = _channelManager.GetChannel(channelName); if (channel == null) { throw new InvalidOperationException("Unknown channel: " + channelName); } var user = channel.GetUser(AccessLevelHelper.GetNick(pid.Nick)); if (user != null) { if (!channel.RemoveUser(AccessLevelHelper.GetNick(pid.Nick))) { throw new InvalidOperationException("Failed to remove user from channel"); } InvokeCallback("UserLeftChannel", channel, user); } }
private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context) { var typeDeclaration = (TypeDeclarationSyntax)context.Node; var members = typeDeclaration.Members; var previousFieldConstant = true; var previousAccessLevel = AccessLevel.NotSpecified; foreach (var member in members) { var field = member as FieldDeclarationSyntax; if (field == null) { continue; } bool currentFieldConstant = field.Modifiers.Any(SyntaxKind.ConstKeyword); var currentAccessLevel = AccessLevelHelper.GetAccessLevel(field.Modifiers); currentAccessLevel = currentAccessLevel == AccessLevel.NotSpecified ? AccessLevel.Private : currentAccessLevel; if (currentAccessLevel == previousAccessLevel && !previousFieldConstant && currentFieldConstant) { context.ReportDiagnostic(Diagnostic.Create(Descriptor, NamedTypeHelpers.GetNameOrIdentifierLocation(member), AccessLevelHelper.GetName(currentAccessLevel))); } previousFieldConstant = currentFieldConstant; previousAccessLevel = currentAccessLevel; } }
private void OnJoin(PacketInvocationData pid) { var channelName = pid.Content.Substring(pid.Content.IndexOf(':') + 1); var channel = _channelManager.GetChannel(channelName); bool isMyself = string.Compare(pid.Nick, MyNick, true) == 0; // myself if (channel == null) { if (!isMyself) // do not care about JOIN in other channels that we're not in { return; } channel = _channelManager.AddChannel(channelName); } var user = channel.AddUser( AccessLevelHelper.GetAccessLevel(pid.Nick), AccessLevelHelper.GetNick(pid.Nick)); if (isMyself) { InvokeCallback("MeJoinedChannel", channel); } else { InvokeCallback("UserJoinedChannel", channel, user); } }
private void OnPrivateMessage(PacketInvocationData pid) { var msg = pid.Content; int pos; if ((pos = msg.IndexOf(" :")) == -1) { throw new Exception("Malformed PRIVMSG"); } var channelName = msg.Substring(0, pos); msg = msg.Substring(pos + 2); if (channelName.Length < 2) { throw new Exception("Malformed PRIVMSG"); } var channel = _channelManager.GetChannel(channelName); if (channel == null) { throw new Exception("PRIVMSG - Channel not found: " + channelName); } var user = channel.GetUser(AccessLevelHelper.GetNick(pid.Nick)); if (user == null) { throw new Exception("PRIVMSG - User '" + AccessLevelHelper.GetNick(pid.Nick) + "' not found in channel: " + channelName); } InvokeCallback("ChannelMessage", channel, user, msg); }
private void On353(PacketInvocationData pid) // userlist { var x_data = pid.Content; int pos; if ((pos = x_data.IndexOf(" = ")) == -1 && // '=' is public channel (pos = x_data.IndexOf(" * ")) == -1 && // '*' is private channel (pos = x_data.IndexOf(" @ ")) == -1) // '@' is secret channel { throw new Exception("Malformed userlist received"); } x_data = x_data.Substring(pos + 3); // #test :Hello Test2 Nice if ((pos = x_data.IndexOf(" :")) == -1) { throw new Exception("Malformed userlist received"); } var channelName = x_data.Substring(0, pos); if (channelName.Length < 2) { throw new Exception("Malformed userlist received"); } var channel = _channelManager.GetChannel(channelName); if (channel == null) { throw new Exception("Unknown channel in 353 packet: " + channelName); } x_data = x_data.Substring(pos + 2); if (!x_data.EndsWith(" ")) // otherwise we dont get the last name ... { x_data = x_data + ' '; } while ((pos = x_data.IndexOf(' ')) != -1) { var rawnick = x_data.Substring(0, pos); x_data = x_data.Substring(pos + 1); string nick; AccessLevel access; AccessLevelHelper.Strip(rawnick, out nick, out access); if (nick.Length > 0 && string.Compare(nick, MyNick, true) != 0) { Debug.WriteLine(string.Format("AddUser '{0}' in channel '{1}'", nick, channelName)); IrcChannelUser user; if ((user = channel.AddUser(access, nick)) != null) { InvokeCallback("UserJoinedChannel", channel, user); } } } }
/// <summary> /// Initializes a new instance of the <see cref="MemberOrderHelper"/> struct. /// </summary> /// <param name="member">The member to wrap.</param> public MemberOrderHelper(MemberDeclarationSyntax member) { this.Member = member; var modifiers = member.GetModifiers(); this.modifierFlags = GetModifierFlags(modifiers); this.accessibilty = AccessLevelHelper.GetAccessLevel(modifiers); }
private static void HandleMemberList(SyntaxNodeAnalysisContext context, SyntaxList <MemberDeclarationSyntax> members, AccessLevel defaultAccessLevel) { var previousSyntaxKind = SyntaxKind.None; var previousAccessLevel = AccessLevel.NotSpecified; var previousMemberStatic = true; foreach (var member in members) { var currentSyntaxKind = member.Kind(); currentSyntaxKind = currentSyntaxKind == SyntaxKind.EventFieldDeclaration ? SyntaxKind.EventDeclaration : currentSyntaxKind; var modifiers = member.GetModifiers(); var currentMemberStatic = modifiers.Any(SyntaxKind.StaticKeyword); var currentMemberConst = modifiers.Any(SyntaxKind.ConstKeyword); AccessLevel currentAccessLevel; if ((currentSyntaxKind == SyntaxKind.ConstructorDeclaration && modifiers.Any(SyntaxKind.StaticKeyword)) || (currentSyntaxKind == SyntaxKind.MethodDeclaration && (member as MethodDeclarationSyntax)?.ExplicitInterfaceSpecifier != null) || (currentSyntaxKind == SyntaxKind.PropertyDeclaration && (member as PropertyDeclarationSyntax)?.ExplicitInterfaceSpecifier != null) || (currentSyntaxKind == SyntaxKind.IndexerDeclaration && (member as IndexerDeclarationSyntax)?.ExplicitInterfaceSpecifier != null)) { currentAccessLevel = AccessLevel.Public; } else { currentAccessLevel = AccessLevelHelper.GetAccessLevel(member.GetModifiers()); currentAccessLevel = currentAccessLevel == AccessLevel.NotSpecified ? defaultAccessLevel : currentAccessLevel; } if (currentSyntaxKind == previousSyntaxKind && currentAccessLevel == previousAccessLevel && !previousMemberStatic && currentMemberStatic && !currentMemberConst) { context.ReportDiagnostic( Diagnostic.Create( Descriptor, NamedTypeHelpers.GetNameOrIdentifierLocation(member), AccessLevelHelper.GetName(currentAccessLevel), MemberNames[currentSyntaxKind])); } previousSyntaxKind = currentSyntaxKind; previousAccessLevel = currentAccessLevel; previousMemberStatic = currentMemberStatic || currentMemberConst; } }
/// <summary> /// Initializes a new instance of the <see cref="MemberOrderHelper"/> struct. /// </summary> /// <param name="member">The member to wrap.</param> /// <param name="checks">The element ordering checks.</param> internal MemberOrderHelper(MemberDeclarationSyntax member, ElementOrderingChecks checks) { this.Member = member; var modifiers = member.GetModifiers(); var type = member.Kind(); type = type == SyntaxKind.EventFieldDeclaration ? SyntaxKind.EventDeclaration : type; this.elementPriority = checks.ElementType ? TypeMemberOrder.IndexOf(type) : 0; this.modifierFlags = GetModifierFlags(modifiers, checks); if (checks.AccessLevel) { if ((type == SyntaxKind.ConstructorDeclaration && this.modifierFlags.HasFlag(ModifierFlags.Static)) || (type == SyntaxKind.MethodDeclaration && ((MethodDeclarationSyntax)member).ExplicitInterfaceSpecifier != null) || (type == SyntaxKind.PropertyDeclaration && ((PropertyDeclarationSyntax)member).ExplicitInterfaceSpecifier != null) || (type == SyntaxKind.IndexerDeclaration && ((IndexerDeclarationSyntax)member).ExplicitInterfaceSpecifier != null)) { this.accessibilty = AccessLevel.Public; } else { this.accessibilty = AccessLevelHelper.GetAccessLevel(modifiers); if (this.accessibilty == AccessLevel.NotSpecified) { if (member.Parent.IsKind(SyntaxKind.CompilationUnit) || member.Parent.IsKind(SyntaxKind.NamespaceDeclaration)) { this.accessibilty = AccessLevel.Internal; } else { this.accessibilty = AccessLevel.Private; } } } } else { this.accessibilty = AccessLevel.Public; } }
private static void HandleMemberList(SyntaxNodeAnalysisContext context, ImmutableArray <OrderingTrait> elementOrder, int accessibilityIndex, SyntaxList <MemberDeclarationSyntax> members) { var previousSyntaxKind = SyntaxKind.None; var previousAccessLevel = AccessLevel.NotSpecified; bool previousIsConst = false; bool previousIsReadonly = false; bool previousIsStatic = false; foreach (var member in members) { var currentSyntaxKind = member.Kind(); currentSyntaxKind = currentSyntaxKind == SyntaxKind.EventFieldDeclaration ? SyntaxKind.EventDeclaration : currentSyntaxKind; // if the SyntaxKind of this member (e.g. SyntaxKind.IncompleteMember) will not // be handled, skip early. if (!MemberKinds.Contains(currentSyntaxKind)) { continue; } var modifiers = member.GetModifiers(); AccessLevel currentAccessLevel = MemberOrderHelper.GetAccessLevelForOrdering(member, modifiers); bool currentIsConst = modifiers.Any(SyntaxKind.ConstKeyword); bool currentIsReadonly = modifiers.Any(SyntaxKind.ReadOnlyKeyword); bool currentIsStatic = modifiers.Any(SyntaxKind.StaticKeyword); if (previousAccessLevel != AccessLevel.NotSpecified) { bool compareAccessLevel = true; for (int j = 0; compareAccessLevel && j < accessibilityIndex; j++) { switch (elementOrder[j]) { case OrderingTrait.Kind: if (previousSyntaxKind != currentSyntaxKind) { compareAccessLevel = false; } continue; case OrderingTrait.Constant: if (previousIsConst != currentIsConst) { compareAccessLevel = false; } continue; case OrderingTrait.Readonly: if (previousIsReadonly != currentIsReadonly) { compareAccessLevel = false; } continue; case OrderingTrait.Static: if (previousIsStatic != currentIsStatic) { compareAccessLevel = false; } continue; case OrderingTrait.Accessibility: default: continue; } } if (compareAccessLevel && currentAccessLevel > previousAccessLevel) { context.ReportDiagnostic( Diagnostic.Create( Descriptor, NamedTypeHelpers.GetNameOrIdentifierLocation(member), AccessLevelHelper.GetName(currentAccessLevel), AccessLevelHelper.GetName(previousAccessLevel))); } } previousSyntaxKind = currentSyntaxKind; previousAccessLevel = currentAccessLevel; previousIsConst = currentIsConst; previousIsReadonly = currentIsReadonly; previousIsStatic = currentIsStatic; } }
private static void HandleMemberList(SyntaxNodeAnalysisContext context, ImmutableArray <OrderingTrait> elementOrder, int staticIndex, SyntaxList <MemberDeclarationSyntax> members) { var previousSyntaxKind = SyntaxKind.None; var previousAccessLevel = AccessLevel.NotSpecified; var previousMemberStatic = true; var previousMemberConstant = false; var previousMemberReadonly = false; foreach (var member in members) { var modifiers = member.GetModifiers(); var currentSyntaxKind = member.Kind(); currentSyntaxKind = currentSyntaxKind == SyntaxKind.EventFieldDeclaration ? SyntaxKind.EventDeclaration : currentSyntaxKind; var currentAccessLevel = MemberOrderHelper.GetAccessLevelForOrdering(member, modifiers); bool currentMemberConstant = modifiers.Any(SyntaxKind.ConstKeyword); bool currentMemberReadonly = currentMemberConstant || modifiers.Any(SyntaxKind.ReadOnlyKeyword); bool currentMemberStatic = currentMemberConstant || modifiers.Any(SyntaxKind.StaticKeyword); bool compareStatic = true; for (int j = 0; compareStatic && j < staticIndex; j++) { switch (elementOrder[j]) { case OrderingTrait.Accessibility: if (currentAccessLevel != previousAccessLevel) { compareStatic = false; } continue; case OrderingTrait.Readonly: if (currentMemberReadonly != previousMemberReadonly) { compareStatic = false; } continue; case OrderingTrait.Constant: if (currentMemberConstant != previousMemberConstant) { compareStatic = false; } continue; case OrderingTrait.Kind: if (previousSyntaxKind != currentSyntaxKind) { compareStatic = false; } continue; case OrderingTrait.Static: default: continue; } } if (compareStatic) { if (currentMemberStatic && !previousMemberStatic) { context.ReportDiagnostic( Diagnostic.Create( Descriptor, NamedTypeHelpers.GetNameOrIdentifierLocation(member), AccessLevelHelper.GetName(currentAccessLevel))); } } previousSyntaxKind = currentSyntaxKind; previousAccessLevel = currentAccessLevel; previousMemberStatic = currentMemberStatic; previousMemberConstant = currentMemberConstant; previousMemberReadonly = currentMemberReadonly; } }
public void AddChatMessage(DateTime date, IrcChannel channel, IrcChannelUser user, string message, bool addSender) { RichTextBox chat = null; foreach (var tabobj in ui_TabControl.Items) { if (string.Compare(((TabItem)tabobj).Header.ToString(), channel.Name) == 0) { chat = (RichTextBox)((TabItem)tabobj).Content; break; } } var flowDocument = chat.Document; // #1585b5 Brush nameColor = new SolidColorBrush(Color.FromArgb(0xff, 0x15, 0x85, 0xb5));; if (AccessLevelHelper.GetNick(user.Nick) == Configuration.Nick) { nameColor = new SolidColorBrush(Colors.Orange); } if (flowDocument.Blocks.Count > 0) { if (Configuration.LineSplitMessages || addSender) { var panel = new StackPanel() { Orientation = Orientation.Vertical, Margin = new Thickness(0, 3, 0, 0) }; panel.Children.Add(new Rectangle() { HorizontalAlignment = HorizontalAlignment.Stretch, Fill = new SolidColorBrush(Color.FromArgb(0xff, 70, 70, 70)), Height = 1 }); flowDocument.Blocks.Add(new BlockUIContainer(panel)); } else { flowDocument.Blocks.Add(new BlockUIContainer(new StackPanel() { Margin = new Thickness(0, 5, 0, 0) })); } } var grid = new Grid(); grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(80) }); // nick grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); // chat message grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50) }); // time stamp var textBlock = new TextBlock() { Text = addSender ? AccessLevelHelper.GetNick(user.Nick) : "", FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Thickness(0, 0, 10, 0), TextWrapping = TextWrapping.Wrap }; if (nameColor != null) { textBlock.Foreground = nameColor; } Grid.SetColumn(textBlock, 0); grid.Children.Add(textBlock); textBlock = new TextBlock() { Text = message, TextWrapping = TextWrapping.Wrap }; Grid.SetColumn(textBlock, 1); grid.Children.Add(textBlock); textBlock = new TextBlock() { Text = date.ToString("HH:mm:ss"), HorizontalAlignment = HorizontalAlignment.Right, TextWrapping = TextWrapping.Wrap }; Grid.SetColumn(textBlock, 2); grid.Children.Add(textBlock); flowDocument.Blocks.Add(new BlockUIContainer(grid)); chat.ScrollToEnd(); }