CSS智能感知无法在Visual Studio 2012旗舰版工作M​​VC 4项目 [英] CSS Intellisense not working for MVC 4 project in Visual Studio 2012 Ultimate

查看:535
本文介绍了CSS智能感知无法在Visual Studio 2012旗舰版工作M​​VC 4项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建了一个全新的Visual Studio 2012旗舰版SP2 MVC4项目,但无法获得CSS类选择智能感知工作?

Have created a brand new Visual Studio 2012 Ultimate SP2 MVC4 project but unable to get CSS class selector intellisense to work?

当我键入< P类=M ....我应该得到的类MyClass的出现在智能感知下拉列表中,但没有任何反应。

When I type <p class="m" .... I should get the class "myClass" appearing in intellisense dropdown but nothing happens.

我下面列出的文件是: \\浏览\\共享\\ _Layout.cshtml

The file I have listed below is: \Views\Shared\_Layout.cshtml

任何想法?

编辑:已经重新安装上全新的窗口VS2012 7系统(在Mac OSX上使用Parallels 8),并以同样的方式仍然表现。似乎也为MVC 3项目中的相同。

Have re-installed VS2012 on brand new windows 7 system (running on Mac OSX parallels 8) and still acting in the same way. Also seems the same for MVC 3 projects.

扩展:

Extensions installed:

推荐答案

我尝试了所有上述补救措施和建议。这些都不在我的环境中工作。据微软(在Microsoft Connect的错误号781048),但还没有实现的MVC /剃刀文件CSS类智能感知,但是,这其中包括在未来的版本都在工作。

I tried all the above mentioned remedies and suggestions. None of these worked in my environment. According to Microsoft (Under Microsoft connect's bug id 781048), they have not implemented CSS class intellisense for MVC/Razor files but are working on including this in a future release.

我有延长VS2012的智能感知,增加了一个解决方案,将增加智能感知到你的VS2012环境10分钟的网络直播例如:一个Visual工作室智能感知扩展

I have a 10 minute webcast example of extending VS2012 intellisense that adds one solution that will add intellisense to your VS2012 environment: a Visual Studio Intellisense Extension

网上直播使用MEF扩展Visual Studio中添加扫描CSS类名当前加载的项目添加为一个智能感知完成集的智能感知完成源。这里是CSS完成源类:

The webcast uses MEF to extend Visual Studio to add an intellisense completion source that scans the currently loaded project for CSS class names to add as an intellisense completion set. Here is the css completion source class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.Utilities;
using EnvDTE;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections.Specialized;

namespace CssClassIntellisense
{
   internal class cssClassList
   {
      public string cssFileName { get; set; } //Intellisense Statement Completion Tab Name

      public HashSet<string> cssClasses { get; set; }
   }

   internal class CssClassCompletionSource : ICompletionSource
   {
    private CssClassCompletionSourceProvider m_sourceProvider;
    private ITextBuffer m_textBuffer;
    private List<Completion> m_compList;
    private Project m_proj;
    private string m_pattern = @"(?<=\.)[A-Za-z0-9_-]+(?=\ {|{|,|\ )";
    private bool m_isDisposed;

    //constructor
    public CssClassCompletionSource(CssClassCompletionSourceProvider sourceProvider, ITextBuffer textBuffer, Project proj)
    {
        m_sourceProvider = sourceProvider;
        m_textBuffer = textBuffer;
        m_proj = proj;
    }

    public void AugmentCompletionSession(ICompletionSession session, IList<CompletionSet> completionSets)
    {

        ITextSnapshot snapshot = session.TextView.TextSnapshot;
        SnapshotPoint currentPoint = (SnapshotPoint)session.GetTriggerPoint(snapshot);

        if (TargetAttribute.Inside(currentPoint))
        {
            var hash = new List<cssClassList>();
            //read any .css project file to get a distinct list of class names
            if (m_proj != null)
                foreach (ProjectItem _item in m_proj.ProjectItems)
                {
                    getCssFiles(_item, hash);
                }

            //Scan Current Editor's text buffer for any inline css class names 
            cssClassList cssclasslist = ScanTextForCssClassName(
                    "Inline", snapshot.GetText());

            //If file had any css class names add to hash of files with css class names
            if (cssclasslist != null)
                hash.Add(cssclasslist);

            var _tokenSpanAtPosition = FindTokenSpanAtPosition(session.GetTriggerPoint(m_textBuffer), session);

            foreach (cssClassList _cssClassList in hash)
            {
                m_compList = new List<Completion>();
                foreach (string str in _cssClassList.cssClasses.OrderBy(x => x))  //alphabetic sort
                    m_compList.Add(new Completion(str, str, str, null, null));

                completionSets.Add(new CompletionSet(
                    _cssClassList.cssFileName,    //the non-localized title of the tab 
                    _cssClassList.cssFileName,    //the display title of the tab
                    _tokenSpanAtPosition,
                    m_compList,
                    null));

            }
        }
    }

    private ITrackingSpan FindTokenSpanAtPosition(ITrackingPoint point, ICompletionSession session)
    {
        SnapshotPoint currentPoint = (session.TextView.Caret.Position.BufferPosition) - 1;
        ITextStructureNavigator navigator = m_sourceProvider.NavigatorService.GetTextStructureNavigator(m_textBuffer);
        TextExtent extent = navigator.GetExtentOfWord(currentPoint);
        return currentPoint.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
    }


    private void getCssFiles(ProjectItem proj, List<cssClassList> hash)
    {
        foreach (ProjectItem _item in proj.ProjectItems)
        {
            if (_item.Name.EndsWith(".css") &&
                !_item.Name.EndsWith(".min.css"))
            {
                //Scan File's text contents for css class names
                cssClassList cssclasslist = ScanTextForCssClassName(
                    _item.Name.Substring(0, _item.Name.IndexOf(".")),
                    System.IO.File.ReadAllText(_item.get_FileNames(0))
                    );

                //If file had any css class names add to hash of files with css class names
                if (cssclasslist != null)
                    hash.Add(cssclasslist);
            }
            //recursively scan any subdirectory project files
            if (_item.ProjectItems.Count > 0)
                getCssFiles(_item, hash);
        }
    }

    private cssClassList ScanTextForCssClassName(string FileName, string TextToScan)
    {

        Regex rEx = new Regex(m_pattern);
        MatchCollection matches = rEx.Matches(TextToScan);
        cssClassList cssclasslist = null;

        if (matches.Count > 0)
        {
            //create css class file object to hold the list css class name that exists in this file
            cssclasslist = new cssClassList();
            cssclasslist.cssFileName = FileName;
            cssclasslist.cssClasses = new HashSet<string>();

        }

        foreach (Match match in matches)
        {
            //creat a unique list of css class names
            if (!cssclasslist.cssClasses.Contains(match.Value))
                cssclasslist.cssClasses.Add(match.Value);
        }

        return cssclasslist;
    }

    public void Dispose()
    {
        if (!m_isDisposed)
        {
            GC.SuppressFinalize(this);
            m_isDisposed = true;
        }
    }
}

}

作为一个仅供参考,您也可以使用ReSharper的解决这个问题。但是,这是一个需要购买的Visual Studio

As an FYI, you can also address this issue using Resharper. But that is a 3rd party product that needs to be purchased for Visual Studio

这篇关于CSS智能感知无法在Visual Studio 2012旗舰版工作M​​VC 4项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆