Skip to content
Permalink
Browse files

[VSPackage] Various fixes so that shader highlighting/goto works

  • Loading branch information
xen2 committed Dec 17, 2018
1 parent 2eba465 commit 84bad0265a2a74c350bc01bc8ee27efcdcd94168
@@ -10,10 +10,11 @@
using Xenko.VisualStudio.Commands.Shaders;
using Xenko.Core.Shaders.Ast;
using Xenko.Core.Shaders.Utility;
using Xenko.Core;

namespace Xenko.VisualStudio.Commands
{
public class XenkoCommands : IXenkoCommands
public class XenkoCommands : IXenkoCommands, IXenkoCommands2
{
public void Initialize(string xenkoSdkDir)
{
@@ -39,12 +40,17 @@ public byte[] GenerateShaderKeys(string inputFileName, string inputFileContent)
}

public RawShaderNavigationResult AnalyzeAndGoToDefinition(string sourceCode, RawSourceSpan span)
{
return AnalyzeAndGoToDefinition(null, sourceCode, span);
}

public RawShaderNavigationResult AnalyzeAndGoToDefinition(string projectPath, string sourceCode, RawSourceSpan span)
{
var rawResult = new RawShaderNavigationResult();

var navigation = new ShaderNavigation();

var shaderDirectories = CollectShadersDirectories(null);
var shaderDirectories = CollectShadersDirectories(projectPath);

if (span.File != null)
{
@@ -102,13 +108,12 @@ private List<string> CollectShadersDirectories(string packagePath)
{
if (packagePath == null)
{
//packagePath = PackageStore.Instance.DefaultPackage.FullPath;
packagePath = PackageStore.Instance.GetPackageFileName("Xenko.Engine", new PackageVersionRange(new PackageVersion(XenkoVersion.NuGetVersion)));
}

var defaultLoad = PackageLoadParameters.Default();
defaultLoad.AutoCompileProjects = false;
defaultLoad.AutoLoadTemporaryAssets = false;
defaultLoad.ConvertUPathToAbsolute = false;
defaultLoad.GenerateNewAssetIds = false;
defaultLoad.LoadAssemblyReferences = false;

@@ -125,12 +130,14 @@ private List<string> CollectShadersDirectories(string packagePath)
var assetsPaths = new List<string>();
foreach (var package in session.Packages)
{
foreach (var folder in package.AssetFolders)
foreach (var assetFolder in package.AssetFolders)
{
var fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(packagePath), folder.Path));

assetsPaths.Add(fullPath);
assetsPaths.AddRange(Directory.EnumerateDirectories(fullPath, "*.*", SearchOption.AllDirectories));
var fullPath = assetFolder.Path.ToWindowsPath();
if (Directory.Exists(fullPath))
{
assetsPaths.Add(fullPath);
assetsPaths.AddRange(Directory.EnumerateDirectories(fullPath, "*.*", SearchOption.AllDirectories));
}
}
}
return assetsPaths;
@@ -34,6 +34,11 @@ public interface IXenkoCommands
RawShaderNavigationResult AnalyzeAndGoToDefinition(string sourceCode, RawSourceSpan span);
}

public interface IXenkoCommands2
{
RawShaderNavigationResult AnalyzeAndGoToDefinition(string projectPath, string sourceCode, RawSourceSpan span);
}

public interface IBuildMonitorCallback
{
void Message(string type, string module, string text);
@@ -205,9 +205,12 @@ public byte[] GenerateShaderKeys(string inputFileName, string inputFileContent)
return remote.GenerateShaderKeys(inputFileName, inputFileContent);
}

public RawShaderNavigationResult AnalyzeAndGoToDefinition(string sourceCode, RawSourceSpan span)
public RawShaderNavigationResult AnalyzeAndGoToDefinition(string projectPath, string sourceCode, RawSourceSpan span)
{

// TODO: We need to know which package is currently selected in order to query all valid shaders
if (remote is IXenkoCommands2 remote2)
return remote2.AnalyzeAndGoToDefinition(projectPath, sourceCode, span);
return remote.AnalyzeAndGoToDefinition(sourceCode, span);
}

@@ -222,6 +225,10 @@ private static Assembly DefaultDomainAssemblyResolve(object sender, ResolveEvent
if (assemblyName.Name == executingAssembly.GetName().Name)
return executingAssembly;

// Necessary to avoid conflicts with Visual Studio NuGet
if (args.Name.StartsWith("NuGet", StringComparison.InvariantCultureIgnoreCase))
return Assembly.Load(assemblyName);

return null;
}

@@ -309,7 +316,8 @@ internal static async Task<PackageInfo> FindXenkoSdkDir(string solution, string
// Xenko 3.1+
else
{
var (request, result) = await RestoreHelper.Restore(new Logger(), packageName, new VersionRange(packageInfo.ExpectedVersion.ToNuGetVersion()));
var logger = new Logger();
var (request, result) = await RestoreHelper.Restore(logger, packageName, new VersionRange(packageInfo.ExpectedVersion.ToNuGetVersion()));
if (result.Success)
{
packageInfo.SdkPaths.AddRange(RestoreHelper.ListAssemblies(request, result));
@@ -323,64 +331,72 @@ internal static async Task<PackageInfo> FindXenkoSdkDir(string solution, string

public class Logger : ILogger
{
private List<string> logs = new List<string>();
private object logLock = new object();
public List<(LogLevel Level, string Message)> Logs { get; } = new List<(LogLevel, string)>();

public void LogDebug(string data)
{
logs.Add(data);
Log(LogLevel.Debug, data);
}

public void LogVerbose(string data)
{
logs.Add(data);
Log(LogLevel.Verbose, data);
}

public void LogInformation(string data)
{
Console.WriteLine(data);
logs.Add(data);
Log(LogLevel.Information, data);
}

public void LogMinimal(string data)
{
logs.Add(data);
Log(LogLevel.Minimal, data);
}

public void LogWarning(string data)
{
logs.Add(data);
Log(LogLevel.Warning, data);
}

public void LogError(string data)
{
logs.Add(data);
Log(LogLevel.Error, data);
}

public void LogInformationSummary(string data)
{
logs.Add(data);
Log(LogLevel.Information, data);
}

public void LogErrorSummary(string data)
{
logs.Add(data);
Log(LogLevel.Error, data);
}

public void Log(LogLevel level, string data)
{
lock (logLock)
{
Debug.WriteLine($"[{level}] {data}");
Logs.Add((level, data));
}
}

public Task LogAsync(LogLevel level, string data)
{
Log(level, data);
return Task.CompletedTask;
}

public void Log(ILogMessage message)
{
Log(message.Level, message.Message);
}

public Task LogAsync(ILogMessage message)
{
Log(message);
return Task.CompletedTask;
}
}
@@ -36,6 +36,8 @@

using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
using VsShell = Microsoft.VisualStudio.Shell.VsShellUtilities;
using EnvDTE;
using Xenko.VisualStudio;

namespace NShader
{
@@ -267,7 +269,7 @@ public override void OnIdle(bool periodic)
{
try
{
var result = XenkoCommandsProxy.GetProxy().AnalyzeAndGoToDefinition(text, new RawSourceSpan(sourcePath, 1, 1));
var result = AnalyzeAndGoToDefinition(text, new RawSourceSpan(sourcePath, 1, 1));
OutputAnalysisMessages(result, source);
}
catch (Exception ex)
@@ -286,6 +288,19 @@ public override void OnIdle(bool periodic)
base.OnIdle(periodic);
}

public RawShaderNavigationResult AnalyzeAndGoToDefinition(string text, RawSourceSpan span)
{
// Try to locate containing project
var dte = (DTE)GetService(typeof(DTE));
var projectItem = dte.Solution.FindProjectItem(span.File);
string projectFile = null;
if (projectItem != null && projectItem.ContainingProject != null && !string.IsNullOrEmpty(projectItem.ContainingProject.FileName))
{
projectFile = projectItem.ContainingProject.FileName;
}
return XenkoCommandsProxy.GetProxy().AnalyzeAndGoToDefinition(projectFile, text, span);
}

private NShaderSource GetCurrentNShaderSource()
{
IVsTextView vsTextView = this.LastActiveTextView;
@@ -88,8 +88,7 @@ private void AnalyzeAndGoToDefinition()
Column = column + 1,
Line = line + 1
};
var result = remoteCommands.AnalyzeAndGoToDefinition(text, location);

var result = langService.AnalyzeAndGoToDefinition(text, location);
langService.OutputAnalysisAndGotoLocation(result, TextView);
}
catch (Exception)
@@ -6,6 +6,7 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Shell;
using Xenko.VisualStudio.CodeGenerator;
using Xenko.VisualStudio.Commands;
@@ -30,8 +31,11 @@ protected override byte[] GenerateCode(string inputFileName, string inputFileCon
{
try
{
var remoteCommands = XenkoCommandsProxy.GetProxy();
return remoteCommands.GenerateShaderKeys(inputFileName, inputFileContent);
return System.Threading.Tasks.Task.Run(() =>
{
var remoteCommands = XenkoCommandsProxy.GetProxy();
return remoteCommands.GenerateShaderKeys(inputFileName, inputFileContent);
}).Result;
}
catch (Exception ex)
{
@@ -16,6 +16,8 @@
<TargetFramework>$(TargetFrameworkTool)</TargetFramework>
<XenkoAssemblyProcessor>false</XenkoAssemblyProcessor>
<XenkoBuildTags>WindowsTools</XenkoBuildTags>
<XenkoSkipAutoPack>true</XenkoSkipAutoPack>
<PackageVersion>3.0.1</PackageVersion>
<VSSDKTargetPlatformRegRootSuffix>Xenko</VSSDKTargetPlatformRegRootSuffix>
</PropertyGroup>
<ItemGroup>

0 comments on commit 84bad02

Please sign in to comment.
You can’t perform that action at this time.