qmljsdialect.cpp 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "qmljsdialect.h"

namespace QmlJS {


bool Dialect::isQmlLikeLanguage() const
{
    switch (m_dialect) {
    case Dialect::Qml:
    case Dialect::QmlQtQuick1:
    case Dialect::QmlQtQuick2:
42
    case Dialect::QmlQtQuick2Ui:
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    case Dialect::QmlQbs:
    case Dialect::QmlProject:
    case Dialect::QmlTypeInfo:
    case Dialect::AnyLanguage:
        return true;
    default:
        return false;
    }
}

bool Dialect::isFullySupportedLanguage() const
{
    switch (m_dialect) {
    case Dialect::JavaScript:
    case Dialect::Json:
    case Dialect::Qml:
    case Dialect::QmlQtQuick1:
    case Dialect::QmlQtQuick2:
61
    case Dialect::QmlQtQuick2Ui:
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        return true;
    case Dialect::NoLanguage:
    case Dialect::AnyLanguage:
    case Dialect::QmlQbs:
    case Dialect::QmlProject:
    case Dialect::QmlTypeInfo:
        break;
    }
    return false;
}

bool Dialect::isQmlLikeOrJsLanguage() const
{
    switch (m_dialect) {
    case Dialect::Qml:
    case Dialect::QmlQtQuick1:
    case Dialect::QmlQtQuick2:
79
    case Dialect::QmlQtQuick2Ui:
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    case Dialect::QmlQbs:
    case Dialect::QmlProject:
    case Dialect::QmlTypeInfo:
    case Dialect::JavaScript:
    case Dialect::AnyLanguage:
        return true;
    default:
        return false;
    }
}

QString Dialect::toString() const
{
    switch (m_dialect) {
    case Dialect::JavaScript:
        return QLatin1String("JavaScript");
    case Dialect::Json:
        return QLatin1String("Json");
    case Dialect::Qml:
        return QLatin1String("Qml");
    case Dialect::QmlQtQuick1:
        return QLatin1String("QmlQtQuick1");
    case Dialect::QmlQtQuick2:
        return QLatin1String("QmlQtQuick2");
104 105
    case Dialect::QmlQtQuick2Ui:
        return QLatin1String("QmlQtQuick2Ui");
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    case Dialect::NoLanguage:
        return QLatin1String("NoLanguage");
    case Dialect::AnyLanguage:
        return QLatin1String("AnyLanguage");
    case Dialect::QmlQbs:
        return QLatin1String("QmlQbs");
    case Dialect::QmlProject:
        return QLatin1String("QmlProject");
    case Dialect::QmlTypeInfo:
        break;
    }
    return QLatin1String("QmlTypeInfo");
}

bool Dialect::operator ==(const Dialect &o) const {
    return m_dialect == o.m_dialect;
}

bool Dialect::operator <(const Dialect &o) const {
    return m_dialect < o.m_dialect;
}

Dialect Dialect::mergeLanguages(const Dialect &l1, const Dialect &l2)
{
    if (l1 == Dialect::NoLanguage)
        return l2;
    if (l2 == Dialect::NoLanguage)
        return l1;
    QList<Dialect> ll1 = l1.companionLanguages();
    QList<Dialect> ll2 = l2.companionLanguages();
    bool i1 = ll1.contains(l2);
    bool i2 = ll2.contains(l1);
    if (i1 && i2) {
        if (ll1.size() > ll2.size())
            return l1;
        if (ll2.size() > ll1.size())
            return l2;
        if (l1 < l2)
            return l1;
        return l2;
    }
    if (i1 && !i2)
        return l1;
    if (i2 && !i1)
        return l2;
    QList<Dialect> qmlLangs = Dialect(Qml).companionLanguages();
    if (qmlLangs.contains(l1) && qmlLangs.contains(l2))
        return Dialect::Qml;
    return Dialect::AnyLanguage;
}

void Dialect::mergeLanguage(const Dialect &l2) {
    *this = mergeLanguages(*this, l2);
}

bool Dialect::restrictLanguage(const Dialect &l2)
{
    if (*this == l2)
        return true;
    QList<Dialect> ll1 = companionLanguages();
    QList<Dialect> ll2 = l2.companionLanguages();
    bool i1 = ll1.contains(l2);
    bool i2 = ll2.contains(*this);
    if (i1 && i2) {
        if (ll1.size() < ll2.size())
            return true;
        if (ll2.size() < ll1.size()) {
            *this = l2;
            return true;
        }
        if (m_dialect < l2.m_dialect)
            return true;
        *this = l2;
        return true;
    }
    if (i1 && !i2) {
        *this = l2;
        return true;
    }
    if (i2 && !i1)
        return true;
187 188 189
    qDebug() << toString() << "restrictTo" << l2.toString() << "failed";
    qDebug() << ll1 << ll2;
    qDebug() << i1 << i2;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    QList<Dialect> qmlLangs = Dialect(Qml).companionLanguages();
    if (qmlLangs.contains(*this) && qmlLangs.contains(l2))
        *this = Dialect::Qml;
    *this = Dialect::AnyLanguage;
    return false;
}

QList<Dialect> Dialect::companionLanguages() const
{
    QList<Dialect> langs;
    langs << *this;
    switch (m_dialect) {
    case Dialect::JavaScript:
    case Dialect::Json:
    case Dialect::QmlProject:
    case Dialect::QmlTypeInfo:
        break;
    case Dialect::QmlQbs:
        langs << Dialect::JavaScript;
        break;
    case Dialect::Qml:
211 212
        langs << Dialect::QmlQtQuick1 << Dialect::QmlQtQuick2 << Dialect::QmlQtQuick2Ui
              << Dialect::JavaScript;
213 214 215 216
        break;
    case Dialect::QmlQtQuick1:
        langs << Dialect::Qml << Dialect::JavaScript;
        break;
217 218 219 220 221 222
    case Dialect::QmlQtQuick2:
    case Dialect::QmlQtQuick2Ui:
        langs.clear();
        langs << Dialect::QmlQtQuick2 << Dialect::QmlQtQuick2Ui << Dialect::Qml
              << Dialect::JavaScript;
        break;
223 224 225
    case Dialect::AnyLanguage:
        langs << Dialect::JavaScript << Dialect::Json << Dialect::QmlProject << Dialect:: QmlQbs
              << Dialect::QmlTypeInfo << Dialect::QmlQtQuick1 << Dialect::QmlQtQuick2
226
              << Dialect::QmlQtQuick2Ui << Dialect::Qml;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        break;
    case Dialect::NoLanguage:
        return QList<Dialect>(); // return at least itself?
    }
    if (*this != Dialect::AnyLanguage)
        langs << Dialect::AnyLanguage;
    return langs;
}

uint qHash(const Dialect &o)
{
    return uint(o.dialect());
}

QDebug operator << (QDebug &dbg, const Dialect &dialect)
{
    dbg << dialect.toString();
    return dbg;
}

PathAndLanguage::PathAndLanguage(const Utils::FileName &path, Dialect language)
    : m_path(path), m_language(language)
{ }

bool PathAndLanguage::operator ==(const PathAndLanguage &other) const
{
    return path() == other.path() && language() == other.language();
}

bool PathAndLanguage::operator <(const PathAndLanguage &other) const
{
    if (path() < other.path())
        return true;
    if (path() > other.path())
        return false;
    if (language() == other.language())
        return false;
    bool i1 = other.language().companionLanguages().contains(language());
    bool i2 = language().companionLanguages().contains(other.language());
    if (i1 && !i2)
        return true;
    if (i2 && !i1)
        return false;
    return language() < other.language();
}

QDebug operator << (QDebug &dbg, const PathAndLanguage &pathAndLanguage)
{
    dbg << "{ path:" << pathAndLanguage.path() << " language:" << pathAndLanguage.language().toString() << "}";
    return dbg;
}

bool PathsAndLanguages::maybeInsert(const PathAndLanguage &pathAndLanguage) {
    for (int i = 0; i < m_list.size(); ++i) {
        PathAndLanguage currentElement = m_list.at(i);
        if (currentElement.path() == pathAndLanguage.path()) {
            int j = i;
            do {
                if (pathAndLanguage.language() < currentElement.language()) {
                    if (currentElement.language() == pathAndLanguage.language())
                        return false;
                    break;
                }
                ++j;
                if (j == m_list.length())
                    break;
                currentElement = m_list.at(j);
            } while (currentElement.path() == pathAndLanguage.path());
            m_list.insert(j, pathAndLanguage);
            return true;
        }
    }
    m_list.append(pathAndLanguage);
    return true;
}

void PathsAndLanguages::compact() {
    int oldCompactionPlace = 0;
    Utils::FileName oldPath = m_list.first().path();
    QList<PathAndLanguage> compactedList;
    bool restrictFailed = false;
    for (int i = 1; i < m_list.length(); ++i) {
        Utils::FileName newPath = m_list.at(i).path();
        if (newPath == oldPath) {
            int newCompactionPlace = i - 1;
            compactedList << m_list.mid(oldCompactionPlace, newCompactionPlace - oldCompactionPlace);
            LanguageMerger merger;
            merger.merge(m_list.at(i - 1).language());
            do {
316
                merger.merge(m_list.at(i).language());
317 318 319 320 321 322 323 324 325 326 327 328 329
                if (++i == m_list.length())
                    break;
                newPath = m_list.at(i).path();
            } while (newPath == oldPath);
            oldCompactionPlace = i;
            compactedList << PathAndLanguage(oldPath, merger.mergedLanguage());
            if (merger.restrictFailed())
                restrictFailed = true;
        }
        oldPath = newPath;
    }
    if (oldCompactionPlace == 0)
        return;
330
    compactedList << m_list.mid(oldCompactionPlace);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    if (restrictFailed)
        qCWarning(qmljsLog) << "failed to restrict PathAndLanguages " << m_list;
    m_list = compactedList;
}

void LanguageMerger::merge(Dialect l)
{
    bool restrictSuccedeed = m_specificLanguage.restrictLanguage(l);
    m_specificLanguage.mergeLanguage(m_minimalSpecificLanguage);
    if (!restrictSuccedeed) {
        m_minimalSpecificLanguage = m_specificLanguage;
        m_restrictFailed = true;
    }
}

} // namespace QmlJS