CSS Intellisense not working for MVC 4 project in Visual Studio 2012 Ultimate

后端 未结 4 2098
失恋的感觉
失恋的感觉 2020-12-20 12:36

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

When I type

相关标签:
4条回答
  • 2020-12-20 13:06

    You are not going to get intellisense for CSS in VS2012 for Razor views. There is a workaround to use intellisense. Just create one test view(.aspx) using ASPX view engine and include your css file there. Now intellisense will work in new aspx view. All you have to do is copy paste the css class from aspx to Razor view(.cshtml or .vbhtml). I hope this helps.

    0 讨论(0)
  • 2020-12-20 13:07

    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.

    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

    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;
            }
        }
    }
    

    }

    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

    0 讨论(0)
  • 2020-12-20 13:11

    Is it just CSS intellisense that's failed or has it completely stopped throughout Visual Studio?

    I had a similar issue that effected the whole of my Visual Studio 2012. It was a while back but I remember deleting a folder from my appdata. Take a look at this link, hopefully it will help: http://www.haneycodes.net/visual-studio-2012-intellisense-not-working-solved/

    0 讨论(0)
  • 2020-12-20 13:17

    Try adding Web Essentials 2012 extension for Visual Studio 2012: http://visualstudiogallery.msdn.microsoft.com/07d54d12-7133-4e15-becb-6f451ea3bea6?SRC=VSIDE

    And/Or

    Try adding Microsoft Web Developer Tools extension.

    I have both of these and using your same example the intellisense works like a charm.

    0 讨论(0)
提交回复
热议问题