Exemplo n.º 1
0
        private CodeLens GetModuleReference(ParsedModule module, ISourceFile spec)
        {
            var moduleVersion = string.IsNullOrEmpty(module.Descriptor.Version) ? null : $" (version: {module.Descriptor.Version})";

            // Ideally we want to navigate to the module declaration, but unfortunately we don't keep proper track of that.
            // Therefore we do a quick file existence check, try to find the common name and then fall back to the current spec file.
            var moduleConfigFile = module.Definition.ModuleConfigFile.ToString(PathTable);

            if (!File.Exists(moduleConfigFile))
            {
                moduleConfigFile = Path.Combine(Path.GetDirectoryName(moduleConfigFile), "module.config.dsc");
            }
            else if (!File.Exists(moduleConfigFile))
            {
                // Fall back to the current spec....
                moduleConfigFile = spec.FileName;
            }

            return
                (new CodeLens
            {
                Range = spec.ToRange(),
                Command = new Command
                {
                    Title = FormattableStringEx.I($"Module: {module.Descriptor.Name}{moduleVersion} - {module.Definition.Root.ToString(PathTable)}"),
                    CommandIdentifier = "DScript.openDocument",
                    Arguments = new object[] { UriExtensions.GetUriFromPath(moduleConfigFile) },
                },
            });
        }
Exemplo n.º 2
0
        private static Result <ArrayOrObject <Location, Location>, ResponseError> DefinitionForTemplateExpression(INode node, string name)
        {
            var taggedTemplte     = node.Parent.Cast <ITaggedTemplateExpression>();
            var literal           = taggedTemplte.TemplateExpression.Cast <ILiteralExpression>();
            var interpolationKind = taggedTemplte.GetInterpolationKind();

            // This is p`` or f`` or `r`
            if (interpolationKind == InterpolationKind.FileInterpolation ||
                interpolationKind == InterpolationKind.PathInterpolation ||
                interpolationKind == InterpolationKind.RelativePathInterpolation)
            {
                string fullPath = string.Empty;

                // Exceptions can happen if the string literal contains any invalid path characters (such as * and ?)
                // as well if there is any string interoplation in them such as ${variable} as the $ is also an invalid path
                // character.
                try
                {
                    if (Path.IsPathRooted(literal.Text))
                    {
                        fullPath = literal.Text;
                    }
                    else
                    {
                        fullPath = Path.Combine(Path.GetDirectoryName(node.GetSourceFile().Path.AbsolutePath), literal.Text);
                    }
                }
                catch (ArgumentException)
                {
                    // An ArgumentException from IsPathRooted indicates that the path has invalid characters.
                    // Path.IsPathRooted and Path.Combine throw ArgumentException;
                }

                // This is path or file.
                // They can be absolute or relative to the current file.

                if (!string.IsNullOrEmpty(fullPath))
                {
                    return(Success(new[]
                    {
                        new Location()
                        {
                            Range = PositionExtensions.EmptyRange(),
                            Uri = UriExtensions.GetUriFromPath(fullPath).ToString(),
                        },
                    }));
                }
            }

            return(SilentError());
        }