Skip to content
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

Adding SyntaxContext #2245

Draft
wants to merge 11 commits into
base: develop/3.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions sources/SilkTouch/Clang/ContextCSharpSyntaxRewriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp;

namespace Silk.NET.SilkTouch.Clang;

/// <summary>
/// <see cref="CSharpSyntaxRewriter"/> which contains a queryable current context
/// </summary>
/// <param name="visitIntoStructuredTrivia"></param>
public abstract class ContextCSharpSyntaxRewriter(bool visitIntoStructuredTrivia = false)
: CSharpSyntaxRewriter(visitIntoStructuredTrivia)
{
/// <summary>
/// The current type being visited
/// </summary>
public TypeContainer? CurrentContext { get; internal set; }

/// <summary>
/// The list of the namespaces being used
/// </summary>
public List<string> Usings { get; internal set; } = [];

/// <summary>
/// The current Namespace being visited
/// </summary>
public INamespaceContext? CurrentNamespaceContext { get; internal set; }

/// <summary>
/// The top-level namespace context (global context)
/// </summary>
public INamespaceContext? TopNamespaceContext { get; internal set; }

/// <summary>
/// The namespace of the current context
/// </summary>
public string CurrentNamespace => CurrentNamespaceContext?.FullNamespace ?? string.Empty;

/// <summary>
/// The currently SyntaxContext
/// </summary>
public SyntaxContext? Context { get; internal set; }

/// <summary>
/// The file that is currently being editted
/// </summary>
public string File { get; internal set; } = string.Empty;

/// <summary>
/// Called when a file is started being worked on
/// </summary>
/// <param name="fileName"></param>
public virtual void OnFileStarted(string fileName) { }

/// <summary>
/// Called when a file is finished being worked on
/// </summary>
/// <param name="fileName"></param>
public virtual void OnFileFinished(string fileName) { }

/// <summary>
/// Whether or not this file should be skipped upon visiting
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public virtual bool ShouldSkipFile(string fileName) { return false; }
}
71 changes: 71 additions & 0 deletions sources/SilkTouch/Clang/ContextCSharpSyntaxVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;

namespace Silk.NET.SilkTouch.Clang;

/// <summary>
/// A <see cref="ContextCSharpSyntaxVisitor"/> with metadata for the current <see cref="SyntaxContext"/> Visit method
/// </summary>
public class ContextCSharpSyntaxVisitor : CSharpSyntaxVisitor
{
/// <summary>
/// The current type being visited
/// </summary>
public IBaseTypeContext? CurrentContext { get; internal set; }

/// <summary>
/// The list of the namespaces being used
/// </summary>
public List<string> Usings { get; internal set; } = [];

/// <summary>
/// The current Namespace being visited
/// </summary>
public INamespaceContext? CurrentNamespaceContext { get; internal set; }

/// <summary>
/// The top-level namespace context (global context)
/// </summary>
public INamespaceContext? TopNamespaceContext { get; internal set; }

/// <summary>
/// The namespace of the current context
/// </summary>
public string CurrentNamespace => CurrentNamespaceContext?.FullNamespace ?? string.Empty;

/// <summary>
/// The currently SyntaxContext
/// </summary>
public SyntaxContext? Context { get; internal set; }

/// <summary>
/// The file that is currently being editted
/// </summary>
public string File { get; internal set; } = string.Empty;

/// <summary>
/// Called when a file is started being worked on
/// </summary>
/// <param name="fileName"></param>
public virtual void OnFileStarted(string fileName) { }

/// <summary>
/// Called when a file is finished being worked on
/// </summary>
/// <param name="fileName"></param>
public virtual void OnFileFinished(string fileName) { }

/// <summary>
/// Whether or not this file should be skipped upon visiting
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public virtual bool ShouldSkipFile(string fileName) { return false; }
}
Loading
Loading