Skip to content

Instantly share code, notes, and snippets.

View controlflow's full-sized avatar

Alexander Shvedov controlflow

View GitHub Profile
@controlflow
controlflow / ReflectionTypeVisitor.cs
Last active August 29, 2015 14:25
Simple System.Type visitor to traverse and replace types
using System;
using JetBrains.Annotations;
namespace Smth {
[PublicAPI]
public abstract class ReflectionTypeVisitor<T> {
public virtual T VisitSimpleType(Type type) => default(T);
public virtual T VisitConstructedType([NotNull] Type typeDefinition, [NotNull] Type[] typeArguments) => default(T);
public virtual T VisitTypeParameterType([NotNull] Type typeParameter) => default(T);
public virtual T VisitArrayType([NotNull] Type arrayType) => default(T);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using JetBrains.Annotations;
namespace JetBrains.VsIntegration.ProjectModel.PropertiesExtender
{
public sealed class AggregatedPropertyDescriptor : PropertyDescriptor
{
[NotNull] private readonly IList<PropertyDescriptor> myPropertyDescriptors;
class Foo {
[ProvidesContext] IPredefinedType _predefinedTypes = ...;
void DoSomething(ITreeNode node) {
if (Check(node)) {
var expressionType = node.GetExpressionType();
if (expressionType.Equals(node.GetPredefinedType().Bool) {
// ^^^^^^^^^^^^^^^^^^^^^^^^
// Use aleady provided 'IPredefinedType' value from '_predefinedTypes' field
}
// 1
smth?.Foo() smth.Foo()
?.Foo() .Foo()
.Foo() ?.Foo()
?.Foo() .Foo()
.Foo() ?.Foo()
.Foo(); ?.Foo();
// 2
smth?.Foo() smth.Foo()
var x = e as T;
if (x != null) {
...
} else {
// no usages of 'x' here
}
// no usages of 'x' here too
// =>
T t;
U u;
if ((t = x.P as T) != null &&
(u = t.P as U) != null) {
// ... use u/t ...
}
// =>
if (x.P is T t &&
public static bool IsInContantContext([NotNull] this ITreeNode context)
{
foreach (var containingNode in context.ContainingNodes())
{
switch (containingNode)
{
case IAttribute attribute:
case IConstantDeclaration constant:
case ILocalConstantDeclaration localConstant:
case IEnumMemberDeclaration enumMember:
using System;
using System.Collections.Generic;
public static class C {
public static void M() {
var xs = new Dictionary<int, string>();
foreach ((int id, string key) in xs) {
// ...
// int a = 0, b = 1;
// ...
var typeArgumentListScanKind = ScanTypeArgumentList(nameOptions);
if (typeArgumentListScanKind == ScanTypeArgumentListKind.DefiniteTypeArgumentList) return true;
if (typeArgumentListScanKind == ScanTypeArgumentListKind.PossibleTypeArgumentList)
{
if ((nameOptions & NameOptions.InTypeList) != 0)
{
return true;
}
}
while (myLexer.TokenType == CSharpTokenType.DOT ||
myLexer.TokenType == CSharpTokenType.DOUBLE_COLON)
{
// ...
}
// =>
while (myLexer.TokenType is var tt && (tt == CSharpTokenType.DOT ||
tt == CSharpTokenType.DOUBLE_COLON))