├── .arcconfig ├── CMakeLists.txt ├── COPYING ├── ExtraDesktop.sh ├── Messages.sh ├── README.md ├── app_templates ├── CMakeLists.txt └── console_app │ ├── %{PROJECTDIRNAME}.kdev4 │ ├── console_app.kdevtemplate │ └── main.go ├── buildsystem ├── CMakeLists.txt ├── builddirchooser.cpp ├── builddirchooser.h ├── builddirchooser.ui ├── builder.cpp ├── builder.h ├── buildjob.cpp ├── buildjob.h ├── buildsystem.cpp ├── buildsystem.h ├── buildsystem.json ├── buildsystem.qrc ├── buildsystem.rc ├── executabletargetitem.cpp ├── executabletargetitem.h ├── preferences.cpp ├── preferences.h ├── utils.cpp └── utils.h ├── builtins.go ├── codecompletion ├── CMakeLists.txt ├── completiondebug.cpp ├── completiondebug.h ├── context.cpp ├── context.h ├── items │ ├── completionitem.cpp │ ├── completionitem.h │ ├── functionitem.cpp │ ├── functionitem.h │ ├── importcompletionitem.cpp │ └── importcompletionitem.h ├── model.cpp ├── model.h ├── tests │ ├── CMakeLists.txt │ ├── testcompletion.cpp │ └── testcompletion.h ├── typematch.cpp ├── typematch.h ├── worker.cpp └── worker.h ├── duchain ├── CMakeLists.txt ├── builders │ ├── contextbuilder.cpp │ ├── contextbuilder.h │ ├── declarationbuilder.cpp │ ├── declarationbuilder.h │ ├── typebuilder.cpp │ ├── typebuilder.h │ ├── usebuilder.cpp │ └── usebuilder.h ├── declarations │ ├── functiondeclaration.cpp │ ├── functiondeclaration.h │ ├── functiondefinition.cpp │ └── functiondefinition.h ├── duchaindebug.cpp ├── duchaindebug.h ├── expressionvisitor.cpp ├── expressionvisitor.h ├── goducontext.cpp ├── goducontext.h ├── helper.cpp ├── helper.h ├── navigation │ ├── declarationnavigationcontext.cpp │ ├── declarationnavigationcontext.h │ ├── navigationwidget.cpp │ └── navigationwidget.h ├── tests │ ├── CMakeLists.txt │ ├── testduchain.cpp │ └── testduchain.h └── types │ ├── gochantype.cpp │ ├── gochantype.h │ ├── gofunctiontype.cpp │ ├── gofunctiontype.h │ ├── gointegraltype.cpp │ ├── gointegraltype.h │ ├── gomaptype.cpp │ ├── gomaptype.h │ ├── gostructuretype.cpp │ └── gostructuretype.h ├── godebug.cpp ├── godebug.h ├── gohighlighting.cpp ├── gohighlighting.h ├── golangparsejob.cpp ├── golangparsejob.h ├── gometalinter ├── CMakeLists.txt ├── gometalinter.json ├── gometalinter.qrc ├── gometalinter.rc ├── job.cpp ├── job.h ├── plugin.cpp ├── plugin.h ├── problemmodel.cpp └── problemmodel.h ├── kdevgo.json ├── kdevgoplugin.cpp ├── kdevgoplugin.h └── parser ├── CMakeLists.txt ├── go.g ├── main.cpp ├── parsesession.cpp ├── parsesession.h └── test ├── CMakeLists.txt ├── main.go ├── parsertest.cpp ├── parsertest.h ├── test.file └── utf8.go /.arcconfig: -------------------------------------------------------------------------------- 1 | { 2 | "phabricator.uri" : "https://phabricator.kde.org/project/profile/29/" 3 | } 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(kdevgoplugin) 3 | 4 | find_package (ECM "5.15.0" REQUIRED NO_MODULE) 5 | 6 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH}) 7 | 8 | include(KDECompilerSettings NO_POLICY_SCOPE) 9 | include(ECMAddTests) 10 | include(KDEInstallDirs) 11 | include(KDECMakeSettings) 12 | include(GenerateExportHeader) 13 | include(FeatureSummary) 14 | 15 | set(QT_MIN_VERSION "5.5.0") 16 | find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED Core Widgets Test) 17 | set(KF5_DEP_VERSION "5.15.0") 18 | find_package(KF5 ${KF5_DEP_VERSION} REQUIRED COMPONENTS 19 | ThreadWeaver 20 | TextEditor 21 | I18n 22 | ) 23 | set(KDEVPLATFORM_DEP_VERSION "5.2.80") 24 | find_package(KDevPlatform ${KDEVPLATFORM_DEP_VERSION} CONFIG) 25 | set_package_properties(KDevPlatform PROPERTIES 26 | TYPE REQUIRED 27 | ) 28 | find_package(KDevelop-PG-Qt CONFIG) 29 | set_package_properties(KDevelop-PG-Qt PROPERTIES 30 | TYPE REQUIRED 31 | ) 32 | 33 | add_definitions(-DTRANSLATION_DOMAIN=\"kdevgo\") 34 | 35 | # needed for parser/ module prefix with includes for some minimal namespacing 36 | include_directories( 37 | ${CMAKE_CURRENT_SOURCE_DIR} 38 | ${CMAKE_CURRENT_BINARY_DIR} 39 | ) 40 | 41 | add_subdirectory(app_templates) 42 | add_subdirectory(buildsystem) 43 | add_subdirectory(parser) 44 | add_subdirectory(duchain) 45 | add_subdirectory(codecompletion) 46 | add_subdirectory(gometalinter) 47 | 48 | kdevplatform_add_plugin(kdevgoplugin JSON kdevgo.json SOURCES 49 | kdevgoplugin.cpp 50 | golangparsejob.cpp 51 | gohighlighting.cpp 52 | godebug.cpp 53 | ) 54 | 55 | target_link_libraries(kdevgoplugin 56 | KDev::Interfaces 57 | KDev::Language 58 | KF5::ThreadWeaver 59 | KF5::TextEditor 60 | kdevgoparser 61 | kdevgoduchain 62 | kdevgocompletion 63 | ) 64 | 65 | install(FILES builtins.go DESTINATION ${KDE_INSTALL_DATADIR}/kdev-go) 66 | 67 | feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES) 68 | -------------------------------------------------------------------------------- /ExtraDesktop.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | #This file outputs in a separate line each file with a .desktop syntax 3 | #that needs to be translated but has a non .desktop extension 4 | find -name \*.kdevtemplate -print 5 | -------------------------------------------------------------------------------- /Messages.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | files=`find . -name \*.rc -o -name \*.ui` 3 | if [ "x$files" != "x" ]; then 4 | $EXTRACTRC $files >> rc.cpp 5 | fi 6 | $XGETTEXT `find . -name \*.cc -o -name \*.cpp -o -name \*.h | grep -v '/test/'` -o $podir/kdevgo.pot 7 | rm -f rc.cpp 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | KDevelop Go Language support plugin 2 | ------------------------------------------------- 3 | 4 | This plugin introduces Go language support for KDevelop. Go is a programming language developed by Google, KDevelop is a free and open-source IDE developed by KDevelop community, available on most Unix-like systems and Microsoft Windows. 5 | 6 | Features 7 | -------------------- 8 | **NOTE**: It is still a WIP, so only a part of all Go language features is supported at this time. 9 | Some of the most important features include: 10 | - Code highlighting(currently some of the language constructions are not supported) 11 | - KDevelop navigation widgets(information about type, declaration and so on) 12 | - Code completion(struct and interface members, type methods and package imports) 13 | 14 | **HOWTO install this plugin** 15 | 16 | KDevelop from git is required. 17 | 18 | 1) Follow KDevelop installation instructions [here](https://community.kde.org/KDevelop/HowToCompile_v5) 19 | 20 | 2) Install KDevelop-PG-Qt from your distribution repository or from git: 21 | ``` 22 | git clone git://anongit.kde.org/kdevelop-pg-qt 23 | mkdir kdevelop-pg-qt/build && cd kdevelop-pg-qt/build 24 | cmake -DCMAKE_PREFIX_PATH=`your KDevelop install dir` 25 | -DCMAKE_INSTALL_PREFIX=`your KDevelop install dir` ../ 26 | make && make install 27 | ``` 28 | 29 | 3) Install Go(if not already installed) 30 | 31 | 4) Install Go Plugin 32 | 33 | Download this repository, 34 | ``` 35 | mkdir kdev-go/build && cd kdev-go/build 36 | cmake -DCMAKE_PREFIX_PATH=`your KDevelop install dir` 37 | -DCMAKE_INSTALL_PREFIX=`your KDevelop install dir` ../ 38 | make && make install 39 | ``` 40 | 41 | 42 | **HOWTO use this plugin** 43 | 44 | I recommend following official code organization suggestions for example like [here](http://golang.org/doc/code.html) or [here](http://www.youtube.com/watch?v=XCsL89YtqCs). In that case you will need to set $GOPATH environment variable to the root of your project before starting KDevelop. Plugin can also try to find path to your project automatically, but it can fail. For go standart library to work you need the path to your 'go' binary be in $PATH environment variable, which if you installed Go normally(with your package manager) should already be there. After that just start KDevelop and open your Go project. If plugin doesn't work(e.g. highlighting is disabled) check if it's loaded in KDevelop(Help->Loaded Plugins). If not - check if it's installed for example like this: find 'KDevelop installation path' -name "*kdevgo*". If it is installed and loaded but still not working consider opening an issue on github or contacting me directly at onehundredof@gmail.com. 45 | 46 | For building and executing you can use Go project manager installed as part of Go plugin. See example [here](https://www.youtube.com/watch?v=KxIy53i0RK0). 47 | 48 | Also I should mention that there is a very poor support for debugging. GDB can be used to debug Go programs, but it doesn't work well, as stated here http://golang.org/doc/gdb. As a result you can debug Go programs with KDevelop, but it works only as good as it works with gdb. If you are really desperate, somewhat better results can be achieved with gccgo toolchain http://golang.org/doc/install/gccgo. 49 | 50 | 51 | Implementation details 52 | --------------------------- 53 | **Parser** 54 | Plugin uses KDevelop-PG-Qt for parsing Go source code. Complete Go language grammar was written in accordance with official Go language specification available at http://golang.org/ref/spec. Plugin includes separate application for testing parser, located at parser/go_parser. First(and only) argument to the exec is file name, containing go code. If you find correct go source code, which this parser fails to recognize I strongly suggest you contact me at onehundredof@gmail.com, because fixing grammar by yourself can be tricky. 55 | 56 | **DUChain** 57 | Definition-Use chain code is organized like most other language plugins for KDevelop organize it. DeclarationBuilder currently opens declarations and types of variables, functions, methods, packages and imports. UseBuilder builds uses in almost all kinds of expressions(assignments, conditions and so on). ExpressionVisitor can evaluate type of simple expressions, containing variables, basic literals, function calls, comparisons and so on. Complex literals, like function literals are unsupported for now. If you want to contribute to the project you can look at the grammar and see what parts are still not implemented. 58 | 59 | **Completion** 60 | Completion code is mostly based on completion for KDevelop QmlJS plugin, so you can use that for details. Currently only variable and function names, struct and interface members, methods, and package imports completion is available. 61 | 62 | Road map 63 | ----------------------- 64 | If you want to contribute to the project look at this list. 65 | - Implementing rest of Go language features(complex literals, const declarations and so on) 66 | - Implementing smarter completion(try to match type that is needed by expression or function parameter) 67 | - Writing tests and documentation together with improving overall stability of the plugin 68 | -------------------------------------------------------------------------------- /app_templates/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(apptemplate_DIRS console_app) 2 | 3 | kdevplatform_add_app_templates(${apptemplate_DIRS}) -------------------------------------------------------------------------------- /app_templates/console_app/%{PROJECTDIRNAME}.kdev4: -------------------------------------------------------------------------------- 1 | [Project] 2 | Name=%{APPNAME} 3 | Manager=KDevGoBuildSystem -------------------------------------------------------------------------------- /app_templates/console_app/console_app.kdevtemplate: -------------------------------------------------------------------------------- 1 | # KDE Config File 2 | [General] 3 | Name=Simple Go console application 4 | Name[ca]=Aplicació senzilla de consola en Go 5 | Name[ca@valencia]=Aplicació senzilla de consola en Go 6 | Name[cs]=Jednoduchá konzolová aplikace Go 7 | Name[de]=Einfache Go-Konsole-Anwendung 8 | Name[en_GB]=Simple Go console application 9 | Name[es]=Aplicación de consola Go sencilla 10 | Name[fi]=Yksinkertainen Go-konsolisovellus 11 | Name[fr]=Application console Go simple 12 | Name[gl]=Aplicativo de consola sinxelo en Go 13 | Name[it]=Semplice applicazione Go per console 14 | Name[ko]=간단한 Go 콘솔 프로그램 15 | Name[nl]=Eenvoudige Go console-toepassingen 16 | Name[pl]=Prosta aplikacja Go w konsoli 17 | Name[pt]=Aplicação de consola simples em Go 18 | Name[ru]=Простая консольная программа на Go 19 | Name[sk]=Jednoduchá konzolová Go aplikácia 20 | Name[sv]=Enkelt Go terminalprogram 21 | Name[tr]=Basit bir Go konsol uygulaması 22 | Name[uk]=Проста консольна програма мовою Go 23 | Name[x-test]=xxSimple Go console applicationxx 24 | Name[zh_CN]=简单的 Go 终端程序 25 | Category=Go 26 | Comment=Generate the file structure to start a Go console application 27 | Comment[ca]=Genera l'estructura de fitxers per iniciar una aplicació de consola en Go 28 | Comment[ca@valencia]=Genera l'estructura de fitxers per iniciar una aplicació de consola en Go 29 | Comment[cs]=Vygenerovat strukturu souborů pro započetí konzolové aplikace Go 30 | Comment[de]=Erstellt die Dateistruktur für eine Go-Konsole-Anwendung 31 | Comment[en_GB]=Generate the file structure to start a Go console application 32 | Comment[es]=Generar una estructura de archivos para iniciar una aplicación de consola Go 33 | Comment[fi]=Luo tiedostorakenne Go-konsolisovelluksen aloittamiseksi 34 | Comment[fr]=Générer la structure de fichiers pour lancer une application console Go 35 | Comment[gl]=Xera a estrutura de ficheiros para comezar un aplicativo de consola en Go. 36 | Comment[it]=Genera la struttura di file per avviare un'applicazione Go per console 37 | Comment[ko]=Go 콘솔 프로그램을 실행하는 파일 구조 생성 38 | Comment[nl]=Genereer de bestandsstructuur om een Go console-toepassing te starten 39 | Comment[pl]=Tworzy strukturę plików, aby zacząć aplikację Go w konsoli 40 | Comment[pt]=Gerar a estrutura de ficheiros para iniciar uma aplicação de consola em Go 41 | Comment[sv]=Skapa filstrukturen för att starta ett Go terminalprogram 42 | Comment[uk]=Створити структуру файлів для запуску консольної програми мовою Go 43 | Comment[x-test]=xxGenerate the file structure to start a Go console applicationxx 44 | ShowFilesAfterGeneration=main.go 45 | -------------------------------------------------------------------------------- /app_templates/console_app/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello world!") 7 | } 8 | -------------------------------------------------------------------------------- /buildsystem/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(buildsystem_PART_SRCS 2 | buildsystem.cpp 3 | builder.cpp 4 | buildjob.cpp 5 | executabletargetitem.cpp 6 | utils.cpp 7 | builddirchooser.cpp 8 | preferences.cpp 9 | ) 10 | 11 | ki18n_wrap_ui(buildsystem_PART_SRCS builddirchooser.ui) 12 | 13 | qt5_add_resources(buildsystem_PART_SRCS buildsystem.qrc) 14 | kdevplatform_add_plugin(gobuildsystem JSON buildsystem.json SOURCES ${buildsystem_PART_SRCS}) 15 | generate_export_header(gobuildsystem BASE_NAME gobuildsystem EXPORT_MACRO_NAME KDEVGOBUILDSYSTEM_EXPORT) 16 | target_link_libraries(gobuildsystem 17 | KF5::KIOWidgets 18 | KDev::Interfaces KDev::Project KDev::Util KDev::Language KDev::OutputView KDev::Shell 19 | ) -------------------------------------------------------------------------------- /buildsystem/builddirchooser.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "builddirchooser.h" 12 | 13 | #include "ui_builddirchooser.h" 14 | #include "utils.h" 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace KDevelop; 26 | 27 | GoBuildDirChooser::GoBuildDirChooser(QWidget* parent) 28 | : QDialog(parent) 29 | { 30 | setWindowTitle(i18n("Configure a build directory")); 31 | 32 | m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 33 | m_buttonBox->button(QDialogButtonBox::Ok)->setDefault(true); 34 | connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 35 | connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 36 | 37 | auto mainWidget = new QWidget(this); 38 | auto mainLayout = new QVBoxLayout; 39 | setLayout(mainLayout); 40 | mainLayout->addWidget(mainWidget); 41 | 42 | m_chooserUi = new Ui::GoBuildDirChooser; 43 | m_chooserUi->setupUi(mainWidget); 44 | 45 | m_chooserUi->buildFolder->setMode(KFile::Directory|KFile::ExistingOnly); 46 | mainLayout->addWidget(m_buttonBox); 47 | } 48 | 49 | GoBuildDirChooser::~GoBuildDirChooser() 50 | { 51 | delete m_chooserUi; 52 | } 53 | 54 | KDevelop::Path GoBuildDirChooser::buildFolder() const 55 | { 56 | return Path(m_chooserUi->buildFolder->url()); 57 | } 58 | void GoBuildDirChooser::setBuildFolder(const KDevelop::Path &path) 59 | { 60 | m_chooserUi->buildFolder->setUrl(path.toUrl()); 61 | } 62 | -------------------------------------------------------------------------------- /buildsystem/builddirchooser.h: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef BUILDDIRCHOOSER_H 12 | #define BUILDDIRCHOOSER_H 13 | 14 | #include 15 | #include "gobuildsystem_export.h" 16 | 17 | #include 18 | #include 19 | 20 | class QDialogButtonBox; 21 | 22 | namespace Ui { 23 | class GoBuildDirChooser; 24 | } 25 | namespace KDevelop { 26 | class IProject; 27 | } 28 | 29 | class KDEVGOBUILDSYSTEM_EXPORT GoBuildDirChooser : public QDialog 30 | { 31 | Q_OBJECT 32 | public: 33 | explicit GoBuildDirChooser(QWidget* parent = nullptr); 34 | ~GoBuildDirChooser() override; 35 | 36 | KDevelop::Path buildFolder() const; 37 | void setBuildFolder(const KDevelop::Path &path); 38 | private: 39 | Ui::GoBuildDirChooser* m_chooserUi; 40 | QDialogButtonBox* m_buttonBox; 41 | }; 42 | 43 | 44 | #endif // BUILDDIRCHOOSER_H 45 | -------------------------------------------------------------------------------- /buildsystem/builddirchooser.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | GoBuildDirChooser 4 | 5 | 6 | 7 | 0 8 | 0 9 | 611 10 | 244 11 | 12 | 13 | 14 | 15 | QFormLayout::ExpandingFieldsGrow 16 | 17 | 18 | 0 19 | 20 | 21 | 0 22 | 23 | 24 | 0 25 | 26 | 27 | 0 28 | 29 | 30 | 31 | 32 | Build &directory: 33 | 34 | 35 | Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter 36 | 37 | 38 | buildFolder 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | KUrlRequester 50 | QWidget 51 |
kurlrequester.h
52 |
53 |
54 | 55 | 56 |
57 | -------------------------------------------------------------------------------- /buildsystem/builder.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "builder.h" 12 | 13 | #include "buildjob.h" 14 | #include "buildsystem.h" 15 | #include "utils.h" 16 | 17 | #include 18 | 19 | KJob* GoBuilder::build( KDevelop::ProjectBaseItem *item ) 20 | { 21 | return createJobForAction(item, QStringLiteral("build")); 22 | } 23 | 24 | KJob* GoBuilder::clean( KDevelop::ProjectBaseItem *item ) 25 | { 26 | return createJobForAction(item, QStringLiteral("clean")); 27 | } 28 | 29 | KJob* GoBuilder::install(KDevelop::ProjectBaseItem *item, const QUrl &installPath) 30 | { 31 | Q_UNUSED(installPath) 32 | return createJobForAction(item, QStringLiteral("install")); 33 | } 34 | 35 | KJob* GoBuilder::createJobForAction(KDevelop::ProjectBaseItem *item, const QString &action) const 36 | { 37 | auto bsm = item->project()->buildSystemManager(); 38 | auto buildDir = bsm->buildDirectory(item); 39 | auto job = new GoBuildJob(nullptr, action, buildDir.toUrl(), Go::buildOutputFile(item).toLocalFile()); 40 | job->setWorkingDirectory(buildDir.toUrl()); 41 | return job; 42 | } 43 | -------------------------------------------------------------------------------- /buildsystem/builder.h: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef BUILDER_H 12 | #define BUILDER_H 13 | 14 | #include 15 | 16 | namespace KDevelop { 17 | class ProjectBaseItem; 18 | } 19 | 20 | class GoBuilder: public QObject, public KDevelop::IProjectBuilder 21 | { 22 | Q_OBJECT 23 | Q_INTERFACES(KDevelop::IProjectBuilder) 24 | public: 25 | KJob* build(KDevelop::ProjectBaseItem *item) override; 26 | KJob* clean(KDevelop::ProjectBaseItem *item) override; 27 | KJob* install(KDevelop::ProjectBaseItem *item, const QUrl &installPath) override; 28 | 29 | private: 30 | KJob *createJobForAction(KDevelop::ProjectBaseItem *item, const QString &action) const; 31 | }; 32 | 33 | #endif // BUILDER_H 34 | 35 | -------------------------------------------------------------------------------- /buildsystem/buildjob.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "buildjob.h" 12 | 13 | #include 14 | 15 | using namespace KDevelop; 16 | 17 | GoBuildJob::GoBuildJob(QObject* parent, QString command, QUrl buildDir, QString resultDir) : OutputExecuteJob(parent), m_command(command), m_output(resultDir) 18 | { 19 | setStandardToolView(IOutputView::BuildView); 20 | setFilteringStrategy(new CompilerFilterStrategy(buildDir.toString())); 21 | setWorkingDirectory(buildDir); 22 | setProperties(KDevelop::OutputExecuteJob::NeedWorkingDirectory | KDevelop::OutputExecuteJob::DisplayStderr | KDevelop::OutputExecuteJob::IsBuilderHint); 23 | } 24 | 25 | QStringList GoBuildJob::commandLine() const 26 | { 27 | return {"go", m_command, "-o", m_output}; 28 | } 29 | -------------------------------------------------------------------------------- /buildsystem/buildjob.h: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef BUILDJOB_H 12 | #define BUILDJOB_H 13 | 14 | #include 15 | 16 | class GoBuildJob : public KDevelop::OutputExecuteJob 17 | { 18 | Q_OBJECT 19 | public: 20 | 21 | GoBuildJob(QObject* parent, QString command, QUrl buildDir, QString resultDir); 22 | QStringList commandLine() const override; 23 | private: 24 | QString m_command; 25 | QString m_output; 26 | }; 27 | #endif // BUILDJOB_H 28 | 29 | -------------------------------------------------------------------------------- /buildsystem/buildsystem.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "buildsystem.h" 12 | 13 | #include "builder.h" 14 | #include "executabletargetitem.h" 15 | #include "utils.h" 16 | #include "builddirchooser.h" 17 | #include "preferences.h" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace KDevelop; 26 | 27 | K_PLUGIN_FACTORY_WITH_JSON(BuildSystemFactory, "buildsystem.json", registerPlugin(); ) 28 | 29 | GoBuildSystem::GoBuildSystem( QObject *parent, const QVariantList& args ) 30 | : KDevelop::AbstractFileManagerPlugin( "gobuildsystem", parent ), m_builder(new GoBuilder()) 31 | { 32 | Q_UNUSED(args) 33 | setXMLFile( "buildsystem.rc" ); 34 | 35 | } 36 | 37 | GoBuildSystem::~GoBuildSystem() 38 | { 39 | } 40 | 41 | IProjectBuilder* GoBuildSystem::builder() const 42 | { 43 | return m_builder; 44 | } 45 | 46 | Path::List GoBuildSystem::includeDirectories(KDevelop::ProjectBaseItem*) const 47 | { 48 | return {}; 49 | } 50 | 51 | Path::List GoBuildSystem::frameworkDirectories(KDevelop::ProjectBaseItem*) const 52 | { 53 | return {}; 54 | } 55 | 56 | QHash GoBuildSystem::defines(KDevelop::ProjectBaseItem*) const 57 | { 58 | return {}; 59 | } 60 | 61 | QString GoBuildSystem::extraArguments(KDevelop::ProjectBaseItem*) const 62 | { 63 | return {}; 64 | } 65 | 66 | ProjectTargetItem* GoBuildSystem::createTarget(const QString& target, KDevelop::ProjectFolderItem *parent) 67 | { 68 | Q_UNUSED(target) 69 | Q_UNUSED(parent) 70 | return nullptr; 71 | } 72 | 73 | bool GoBuildSystem::addFilesToTarget(const QList< ProjectFileItem* > &files, ProjectTargetItem* parent) 74 | { 75 | Q_UNUSED( files ) 76 | Q_UNUSED( parent ) 77 | return false; 78 | } 79 | 80 | bool GoBuildSystem::removeTarget(KDevelop::ProjectTargetItem *target) 81 | { 82 | Q_UNUSED( target ) 83 | return false; 84 | } 85 | 86 | bool GoBuildSystem::removeFilesFromTargets(const QList< ProjectFileItem* > &targetFiles) 87 | { 88 | Q_UNUSED( targetFiles ) 89 | return false; 90 | } 91 | 92 | bool GoBuildSystem::hasBuildInfo(KDevelop::ProjectBaseItem* item) const 93 | { 94 | Q_UNUSED(item); 95 | return false; 96 | } 97 | 98 | Path GoBuildSystem::compiler(KDevelop::ProjectTargetItem* p) const 99 | { 100 | Q_UNUSED(p); 101 | return Path("go"); 102 | } 103 | 104 | Path GoBuildSystem::buildDirectory(KDevelop::ProjectBaseItem* item) const 105 | { 106 | auto project = item->project(); 107 | ProjectFolderItem *folder = nullptr; 108 | do { 109 | folder = dynamic_cast(item); 110 | item = item->parent(); 111 | } while (!folder && item); 112 | 113 | if(folder) { 114 | return folder->path(); 115 | } 116 | 117 | return project->path(); 118 | } 119 | 120 | QList GoBuildSystem::targets(KDevelop::ProjectFolderItem*) const 121 | { 122 | return {}; 123 | } 124 | 125 | KDevelop::ProjectFolderItem * GoBuildSystem::createFolderItem(KDevelop::IProject* project, const KDevelop::Path& path, KDevelop::ProjectBaseItem* parent) 126 | { 127 | ProjectBuildFolderItem *item = new KDevelop::ProjectBuildFolderItem(project, path, parent); 128 | new GoExecutableTargetItem(item, item->folderName()); 129 | 130 | return item; 131 | } 132 | 133 | int GoBuildSystem::perProjectConfigPages() const 134 | { 135 | return 1; 136 | } 137 | 138 | KDevelop::ConfigPage* GoBuildSystem::perProjectConfigPage(int number, const KDevelop::ProjectConfigOptions &options, QWidget *parent) 139 | { 140 | if (number == 0) 141 | { 142 | return new GoPreferences(this, options, parent); 143 | } 144 | return nullptr; 145 | } 146 | 147 | KDevelop::ProjectFolderItem *GoBuildSystem::import(KDevelop::IProject *project) 148 | { 149 | auto buildDir = Go::currentBuildDir(project); 150 | if(buildDir.isEmpty()) 151 | { 152 | auto newBuildDir = Path(project->path().parent(), project->name()+"-build"); 153 | GoBuildDirChooser buildDirChooser; 154 | buildDirChooser.setBuildFolder(newBuildDir); 155 | if(buildDirChooser.exec()) 156 | { 157 | newBuildDir = buildDirChooser.buildFolder(); 158 | } 159 | Go::setCurrentBuildDir(project, newBuildDir); 160 | } 161 | return AbstractFileManagerPlugin::import(project); 162 | } 163 | 164 | #include "buildsystem.moc" 165 | -------------------------------------------------------------------------------- /buildsystem/buildsystem.h: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef BUILDSYSTEM_H 12 | #define BUILDSYSTEM_H 13 | 14 | #include 15 | #include 16 | 17 | class GoBuildSystem : public KDevelop::AbstractFileManagerPlugin, 18 | public KDevelop::IBuildSystemManager 19 | { 20 | Q_OBJECT 21 | Q_INTERFACES( KDevelop::IBuildSystemManager ) 22 | public: 23 | 24 | GoBuildSystem(QObject* parent = nullptr, const QVariantList& args = QVariantList()); 25 | 26 | ~GoBuildSystem() override; 27 | 28 | Features features() const override { return Features(Folders | Targets | Files); } 29 | KDevelop::ProjectFolderItem* import(KDevelop::IProject* project) override; 30 | KDevelop::IProjectBuilder* builder() const override; 31 | KDevelop::Path::List includeDirectories(KDevelop::ProjectBaseItem*) const override; 32 | KDevelop::Path::List frameworkDirectories(KDevelop::ProjectBaseItem*) const override; 33 | QHash defines(KDevelop::ProjectBaseItem*) const override; 34 | QString extraArguments(KDevelop::ProjectBaseItem* item) const override; 35 | KDevelop::ProjectTargetItem* createTarget(const QString& target, KDevelop::ProjectFolderItem *parent) override; 36 | bool addFilesToTarget(const QList &files, KDevelop::ProjectTargetItem *parent) override; 37 | bool removeTarget(KDevelop::ProjectTargetItem *target) override; 38 | bool removeFilesFromTargets(const QList&) override; 39 | bool hasBuildInfo(KDevelop::ProjectBaseItem* item) const override; 40 | KDevelop::Path compiler(KDevelop::ProjectTargetItem* p) const override; 41 | KDevelop::Path buildDirectory(KDevelop::ProjectBaseItem*) const override; 42 | QList targets(KDevelop::ProjectFolderItem*) const override; 43 | KDevelop::ProjectFolderItem* createFolderItem(KDevelop::IProject * project, const KDevelop::Path & path, KDevelop::ProjectBaseItem * parent) override; 44 | int perProjectConfigPages() const override; 45 | KDevelop::ConfigPage* perProjectConfigPage(int number, const KDevelop::ProjectConfigOptions& options, QWidget* parent) override; 46 | 47 | private: 48 | KDevelop::IProjectBuilder* m_builder; 49 | }; 50 | #endif // BUILDSYSTEM_H 51 | -------------------------------------------------------------------------------- /buildsystem/buildsystem.json: -------------------------------------------------------------------------------- 1 | { 2 | "KPlugin": { 3 | "Category": "Project Management", 4 | "Description": "Imports Go projects", 5 | "Description[ca@valencia]": "Importa projectes del Go", 6 | "Description[ca]": "Importa projectes del Go", 7 | "Description[cs]": "Importuje projekty Go", 8 | "Description[de]": "Import von Go-Projekten", 9 | "Description[en_GB]": "Imports Go projects", 10 | "Description[es]": "Importa proyectos de Go", 11 | "Description[fi]": "Tuo Go-projekteja", 12 | "Description[fr]": "Importe des projets Go", 13 | "Description[gl]": "Importa proxectos de Go.", 14 | "Description[it]": "Importa progetti Go", 15 | "Description[ko]": "Go 프로젝트 가져오기", 16 | "Description[nl]": "Importeert Go-projecten", 17 | "Description[pl]": "Importuje projekty Go", 18 | "Description[pt]": "Importa projectos de Go", 19 | "Description[sv]": "Importerar Go-projekt", 20 | "Description[uk]": "Імпортування проєктів Go", 21 | "Description[x-test]": "xxImports Go projectsxx", 22 | "Description[zh_TW]": "匯入 Go 專案", 23 | "Icon": "kdevelop", 24 | "Id": "KDevGoBuildSystem", 25 | "Name": "Go Project Manager", 26 | "Name[ca@valencia]": "Gestor del projecte Go", 27 | "Name[ca]": "Gestor del projecte Go", 28 | "Name[cs]": "Správce projektu Go", 29 | "Name[de]": "Go-Projektverwaltung", 30 | "Name[en_GB]": "Go Project Manager", 31 | "Name[es]": "Gestor de proyectos de Go", 32 | "Name[fi]": "Go-projektinhallinta", 33 | "Name[fr]": "Gestionnaire de projets Go", 34 | "Name[gl]": "Xestor de proxectos de Go", 35 | "Name[it]": "Gestore progetto Go", 36 | "Name[ko]": "Go 프로젝트 관리자", 37 | "Name[nl]": "Projectmanager van Go", 38 | "Name[pl]": "Zarządzanie projektem Go", 39 | "Name[pt]": "Gestor de Projectos de Go", 40 | "Name[sk]": "Správca projektu Go", 41 | "Name[sv]": "Gå till projekthanterare", 42 | "Name[tr]": "Go Proje Yöneticisi", 43 | "Name[uk]": "Керування проєктами go", 44 | "Name[x-test]": "xxGo Project Managerxx", 45 | "Name[zh_TW]": "Go 專案管理員", 46 | "ServiceTypes": [ 47 | "KDevelop/Plugin" 48 | ] 49 | }, 50 | "X-KDevelop-FileManager": "Go", 51 | "X-KDevelop-IRequired": [ 52 | "org.kdevelop.IMakeBuilder", 53 | "org.kdevelop.IDefinesAndIncludesManager" 54 | ], 55 | "X-KDevelop-Interfaces": [ 56 | "org.kdevelop.IBuildSystemManager", 57 | "org.kdevelop.IProjectFileManager" 58 | ], 59 | "X-KDevelop-Mode": "NoGUI", 60 | "X-KDevelop-ProjectFilesFilter": [ 61 | "*.go" 62 | ], 63 | "X-KDevelop-ProjectFilesFilterDescription": "Go projects" 64 | } 65 | -------------------------------------------------------------------------------- /buildsystem/buildsystem.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | buildsystem.rc 5 | 6 | 7 | -------------------------------------------------------------------------------- /buildsystem/buildsystem.rc: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Run 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /buildsystem/executabletargetitem.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "executabletargetitem.h" 12 | 13 | #include "utils.h" 14 | 15 | GoExecutableTargetItem::GoExecutableTargetItem(KDevelop::ProjectFolderItem* parent, const QString& name) 16 | : KDevelop::ProjectExecutableTargetItem(parent->project(), name, parent) 17 | {} 18 | 19 | QUrl GoExecutableTargetItem::builtUrl() const 20 | { 21 | return Go::buildOutputFile(parent()).toUrl(); 22 | } 23 | 24 | QUrl GoExecutableTargetItem::installedUrl() const 25 | { 26 | return QUrl(); 27 | } 28 | -------------------------------------------------------------------------------- /buildsystem/executabletargetitem.h: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef EXECUTABLETARGETITEM_H 12 | #define EXECUTABLETARGETITEM_H 13 | 14 | #include 15 | 16 | class GoExecutableTargetItem : public KDevelop::ProjectExecutableTargetItem 17 | { 18 | public: 19 | GoExecutableTargetItem(KDevelop::ProjectFolderItem* parent, const QString& name); 20 | 21 | QUrl builtUrl() const override; 22 | QUrl installedUrl() const override; 23 | }; 24 | 25 | 26 | #endif // EXECUTABLETARGETITEM_H 27 | -------------------------------------------------------------------------------- /buildsystem/preferences.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "preferences.h" 12 | 13 | #include "ui_builddirchooser.h" 14 | #include "utils.h" 15 | 16 | #include 17 | #include 18 | 19 | GoPreferences::GoPreferences(KDevelop::IPlugin *plugin, const KDevelop::ProjectConfigOptions &options, QWidget *parent) 20 | : ConfigPage(plugin, nullptr, parent), m_project(options.project) 21 | { 22 | m_buildDirChooser = new Ui::GoBuildDirChooser; 23 | m_buildDirChooser->setupUi(this); 24 | m_buildDirChooser->buildFolder->setMode(KFile::Directory|KFile::ExistingOnly); 25 | connect(m_buildDirChooser->buildFolder, &KUrlRequester::textChanged, this, &GoPreferences::changed); 26 | reset(); 27 | } 28 | 29 | QString GoPreferences::name() const 30 | { 31 | return QStringLiteral("Go"); 32 | } 33 | 34 | void GoPreferences::reset() 35 | { 36 | m_buildDirChooser->buildFolder->setUrl(Go::currentBuildDir(m_project).toUrl()); 37 | } 38 | void GoPreferences::apply() 39 | { 40 | Go::setCurrentBuildDir(m_project, KDevelop::Path(m_buildDirChooser->buildFolder->url())); 41 | } 42 | GoPreferences::~GoPreferences() 43 | { 44 | delete(m_buildDirChooser); 45 | } 46 | -------------------------------------------------------------------------------- /buildsystem/preferences.h: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef PREFERENCES_H 12 | #define PREFERENCES_H 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | namespace Ui { class GoBuildDirChooser; } 19 | 20 | class GoPreferences : public KDevelop::ConfigPage 21 | { 22 | Q_OBJECT 23 | public: 24 | explicit GoPreferences(KDevelop::IPlugin* plugin, const KDevelop::ProjectConfigOptions& options, QWidget* parent = nullptr); 25 | ~GoPreferences() override; 26 | 27 | QString name() const override; 28 | 29 | void apply() override; 30 | void reset() override; 31 | private: 32 | 33 | KDevelop::IProject* m_project; 34 | Ui::GoBuildDirChooser* m_buildDirChooser; 35 | }; 36 | 37 | #endif // PREFERENCES_H 38 | -------------------------------------------------------------------------------- /buildsystem/utils.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "utils.h" 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | using namespace KDevelop; 21 | 22 | namespace Config 23 | { 24 | static const QString buildDirPathKey = QStringLiteral("Build Directory Path"); 25 | static const QString groupName = QStringLiteral("Go"); 26 | } 27 | 28 | namespace Go 29 | { 30 | 31 | KConfigGroup baseGroup( KDevelop::IProject* project ) 32 | { 33 | return project ? project->projectConfiguration()->group( Config::groupName ) : KConfigGroup(); 34 | } 35 | 36 | KDevelop::Path currentBuildDir(KDevelop::IProject *project) 37 | { 38 | return KDevelop::Path(baseGroup(project).readEntry(Config::buildDirPathKey, QString())); 39 | } 40 | 41 | void setCurrentBuildDir(KDevelop::IProject *project, const KDevelop::Path& path) 42 | { 43 | baseGroup(project).writeEntry(Config::buildDirPathKey, path.toLocalFile()); 44 | } 45 | 46 | KDevelop::Path buildOutputFile(KDevelop::ProjectBaseItem *item) 47 | { 48 | auto buildDir = currentBuildDir(item->project()); 49 | 50 | auto project = item->project(); 51 | ProjectFolderItem *folder = nullptr; 52 | 53 | if(!(folder = dynamic_cast(item))) 54 | { 55 | folder = dynamic_cast(item->parent()); 56 | } 57 | 58 | auto folderPath = project->path().relativePath(folder->path()); 59 | auto result = Path(buildDir.cd(folderPath), folder->folderName()); 60 | 61 | return result; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /buildsystem/utils.h: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef UTILS_H 12 | #define UTILS_H 13 | 14 | #include "gobuildsystem_export.h" 15 | 16 | #include 17 | #include 18 | 19 | namespace Go 20 | { 21 | /** 22 | * @returns the current build dir for the given project or an empty url if none 23 | * has been set by the user. 24 | */ 25 | KDEVGOBUILDSYSTEM_EXPORT KDevelop::Path currentBuildDir(KDevelop::IProject* project); 26 | 27 | /** 28 | * @returns the current build output for the given project item 29 | */ 30 | KDEVGOBUILDSYSTEM_EXPORT KDevelop::Path buildOutputFile(KDevelop::ProjectBaseItem *item); 31 | 32 | /** 33 | * Sets the build dir for the given project 34 | */ 35 | KDEVGOBUILDSYSTEM_EXPORT void setCurrentBuildDir(KDevelop::IProject* project, const KDevelop::Path& path); 36 | } 37 | 38 | #endif // UTILS_H 39 | -------------------------------------------------------------------------------- /codecompletion/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(completion_SRCS 2 | model.cpp 3 | worker.cpp 4 | context.cpp 5 | items/completionitem.cpp 6 | items/functionitem.cpp 7 | items/importcompletionitem.cpp 8 | completiondebug.cpp 9 | typematch.cpp 10 | ) 11 | 12 | add_library(kdevgocompletion SHARED ${completion_SRCS}) 13 | generate_export_header(kdevgocompletion BASE_NAME kdevgocompletion EXPORT_MACRO_NAME KDEVGOCOMPLETION_EXPORT) 14 | target_link_libraries(kdevgocompletion LINK_PRIVATE 15 | KDev::Language 16 | KDev::Interfaces 17 | KDev::Project 18 | kdevgoduchain 19 | kdevgoparser 20 | ) 21 | 22 | install(TARGETS kdevgocompletion DESTINATION ${INSTALL_TARGETS_DEFAULT_ARGS}) 23 | 24 | if(BUILD_TESTING) 25 | add_subdirectory(tests) 26 | endif() 27 | -------------------------------------------------------------------------------- /codecompletion/completiondebug.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include 20 | 21 | Q_LOGGING_CATEGORY(COMPLETION, "kdev-go-completion") -------------------------------------------------------------------------------- /codecompletion/completiondebug.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOCOMPLETIONDEBUG_H 20 | #define GOCOMPLETIONDEBUG_H 21 | 22 | #include 23 | Q_DECLARE_LOGGING_CATEGORY(COMPLETION) 24 | 25 | #endif -------------------------------------------------------------------------------- /codecompletion/context.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGCOMPLETIONCONTEXT_H 20 | #define GOLANGCOMPLETIONCONTEXT_H 21 | 22 | #include 23 | #include 24 | 25 | #include "typematch.h" 26 | #include "kdevgocompletion_export.h" 27 | #include 28 | #include 29 | 30 | namespace go 31 | { 32 | 33 | class KDEVGOCOMPLETION_EXPORT CodeCompletionContext : public KDevelop::CodeCompletionContext 34 | { 35 | public: 36 | CodeCompletionContext(const KDevelop::DUContextPointer& context, const QString& text, 37 | const KDevelop::CursorInRevision& position, int depth = 0); 38 | 39 | QList completionItems(bool& abort, bool fullCompletion = true) override; 40 | 41 | TypeMatch typeToMatch() { return m_typeMatch; } 42 | 43 | private: 44 | //See QmlJS plugin completion for details 45 | struct ExpressionStackEntry { 46 | int startPosition; 47 | int operatorStart; 48 | int operatorEnd; 49 | int commas; 50 | }; 51 | 52 | QStack expressionStack(const QString& expression); 53 | 54 | KDevelop::AbstractType::Ptr lastType(const QString& expression); 55 | 56 | KDevelop::DeclarationPointer lastDeclaration(const QString& expression); 57 | 58 | QList getImportableDeclarations(KDevelop::Declaration *sourceDeclaration); 59 | 60 | QList importAndMemberCompletion(); 61 | 62 | QList normalCompletion(); 63 | 64 | /** 65 | * Creates FunctionCallTips and sets m_typeToMatch 66 | **/ 67 | QList functionCallTips(); 68 | 69 | void setTypeToMatch(); 70 | 71 | QList importCompletion(); 72 | 73 | /** 74 | * Return completion item for declaration. 75 | **/ 76 | KDevelop::CompletionTreeItemPointer itemForDeclaration(QPair declaration); 77 | 78 | /** 79 | * returns true if cursor is in comment and completion is not needed 80 | **/ 81 | bool isInsideCommentOrString(); 82 | bool isImportAndMemberCompletion(); 83 | 84 | TypeMatch m_typeMatch; 85 | QString m_fullText; 86 | QString extractLastExpression(const QString &str); 87 | bool endsWithDot(const QString &str); 88 | }; 89 | } 90 | 91 | #endif -------------------------------------------------------------------------------- /codecompletion/items/completionitem.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGCOMPLETIONITEM_H 20 | #define GOLANGCOMPLETIONITEM_H 21 | 22 | #include 23 | 24 | using namespace KDevelop; 25 | 26 | namespace go 27 | { 28 | 29 | class CompletionItem : public NormalDeclarationCompletionItem 30 | { 31 | public: 32 | CompletionItem(KDevelop::DeclarationPointer decl = KDevelop::DeclarationPointer(), 33 | QExplicitlySharedDataPointer context=QExplicitlySharedDataPointer(), 34 | int inheritanceDepth = 0); 35 | 36 | QVariant data(const QModelIndex& index, int role, const KDevelop::CodeCompletionModel* model) const override; 37 | 38 | private: 39 | QString m_prefix; 40 | }; 41 | 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /codecompletion/items/functionitem.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGFUNCTIONITEM_H 20 | #define GOLANGFUNCTIONITEM_H 21 | 22 | #include "completionitem.h" 23 | 24 | using namespace KDevelop; 25 | 26 | namespace go 27 | { 28 | 29 | class FunctionCompletionItem : public CompletionItem 30 | { 31 | public: 32 | FunctionCompletionItem(KDevelop::DeclarationPointer declaration = KDevelop::DeclarationPointer(), 33 | int depth=0, int atArgument=-1); 34 | 35 | 36 | void executed(KTextEditor::View* view, const KTextEditor::Range& word) override; 37 | QVariant data(const QModelIndex& index, int role, const KDevelop::CodeCompletionModel* model) const override; 38 | KTextEditor::CodeCompletionModel::CompletionProperties completionProperties() const override; 39 | int argumentHintDepth() const override; 40 | int inheritanceDepth() const override; 41 | 42 | private: 43 | int m_depth; 44 | int m_atArgument; 45 | int m_currentArgStart; 46 | int m_currentArgEnd; 47 | QString m_prefix; 48 | QString m_arguments; 49 | }; 50 | 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /codecompletion/items/importcompletionitem.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "importcompletionitem.h" 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | namespace go 26 | { 27 | 28 | ImportCompletionItem::ImportCompletionItem(QString packagename): KDevelop::NormalDeclarationCompletionItem(KDevelop::DeclarationPointer(), 29 | QExplicitlySharedDataPointer(), 0), m_packageName(packagename) 30 | { 31 | } 32 | 33 | QVariant ImportCompletionItem::data(const QModelIndex& index, int role, const KDevelop::CodeCompletionModel* model) const 34 | { 35 | if(role == Qt::DisplayRole && (index.column() == CodeCompletionModel::Name)) 36 | return m_packageName; 37 | if(role == Qt::DisplayRole && (index.column() == CodeCompletionModel::Prefix)) 38 | return "package"; 39 | return NormalDeclarationCompletionItem::data(index, role, model); 40 | } 41 | 42 | void ImportCompletionItem::execute(KTextEditor::View* view, const KTextEditor::Range& word) 43 | { 44 | KTextEditor::Document* document = view->document(); 45 | KTextEditor::Range checkSuffix(word.end().line(), word.end().column(), word.end().line(), document->lineLength(word.end().line())); 46 | QString suffix = "\""; 47 | if(document->text(checkSuffix).startsWith('"')) 48 | suffix.clear(); 49 | document->replaceText(word, m_packageName + suffix); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /codecompletion/items/importcompletionitem.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGIMPORTITEM_H 20 | #define GOLANGIMPORTITEM_H 21 | 22 | #include 23 | 24 | using namespace KDevelop; 25 | 26 | namespace go 27 | { 28 | 29 | class ImportCompletionItem : public KDevelop::NormalDeclarationCompletionItem 30 | { 31 | public: 32 | ImportCompletionItem(QString packagename); 33 | QVariant data(const QModelIndex& index, int role, const KDevelop::CodeCompletionModel* model) const override; 34 | void execute(KTextEditor::View* view, const KTextEditor::Range& word) override; 35 | 36 | private: 37 | QString m_packageName; 38 | }; 39 | 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /codecompletion/model.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "model.h" 20 | 21 | #include "worker.h" 22 | 23 | namespace go 24 | { 25 | 26 | CodeCompletionModel::CodeCompletionModel(QObject* parent): KDevelop::CodeCompletionModel(parent) 27 | { 28 | 29 | } 30 | 31 | KDevelop::CodeCompletionWorker* CodeCompletionModel::createCompletionWorker() 32 | { 33 | return new CodeCompletionWorker(this); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /codecompletion/model.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGCOMPLETIONMODEL_H 20 | #define GOLANGCOMPLETIONMODEL_H 21 | 22 | #include 23 | 24 | #include "kdevgocompletion_export.h" 25 | 26 | namespace go 27 | { 28 | 29 | class KDEVGOCOMPLETION_EXPORT CodeCompletionModel : public KDevelop::CodeCompletionModel 30 | { 31 | public: 32 | CodeCompletionModel(QObject* parent); 33 | protected: 34 | KDevelop::CodeCompletionWorker* createCompletionWorker() override; 35 | }; 36 | 37 | } 38 | 39 | #endif -------------------------------------------------------------------------------- /codecompletion/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ecm_add_test(testcompletion.cpp 2 | TEST_NAME completion 3 | LINK_LIBRARIES 4 | Qt5::Test 5 | KDev::Language 6 | KDev::Tests 7 | kdevgoduchain 8 | kdevgoparser 9 | kdevgocompletion 10 | ) 11 | -------------------------------------------------------------------------------- /codecompletion/tests/testcompletion.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGTESTCOMPLETION_H 20 | #define GOLANGTESTCOMPLETION_H 21 | 22 | #include 23 | 24 | class TestCompletion : public QObject 25 | { 26 | Q_OBJECT 27 | private slots: 28 | void initTestCase(); 29 | void cleanupTestCase(); 30 | void test_basicCompletion(); 31 | void test_functionCallTips_data(); 32 | void test_functionCallTips(); 33 | void test_typeMatching_data(); 34 | void test_typeMatching(); 35 | void test_commentCompletion_data(); 36 | void test_commentCompletion(); 37 | void test_membersCompletion_data(); 38 | void test_membersCompletion(); 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /codecompletion/typematch.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop Go codecompletion support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "typematch.h" 12 | 13 | namespace go 14 | { 15 | 16 | TypeMatch::TypeMatch() : m_singleType(nullptr), m_multipleTypes({}) 17 | { 18 | } 19 | 20 | TypeMatch::TypeMatch(const KDevelop::AbstractType::Ptr &m_singleType, 21 | const QList &m_multipleTypes) 22 | : m_singleType(m_singleType), m_multipleTypes(m_multipleTypes) 23 | { 24 | 25 | } 26 | 27 | KDevelop::AbstractType::Ptr TypeMatch::singleType() const 28 | { 29 | return m_singleType; 30 | } 31 | 32 | QList TypeMatch::multipleTypes() const 33 | { 34 | return m_multipleTypes; 35 | } 36 | 37 | void TypeMatch::setSingleType(const KDevelop::AbstractType::Ptr &singleType) 38 | { 39 | m_singleType = singleType; 40 | } 41 | 42 | void TypeMatch::setMultipleTypes(const QList &multipleTypes) 43 | { 44 | m_multipleTypes = multipleTypes; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /codecompletion/typematch.h: -------------------------------------------------------------------------------- 1 | /* KDevelop Go codecompletion support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef GOLANGTYPEMATCH_H 12 | #define GOLANGTYPEMATCH_H 13 | 14 | #include 15 | #include 16 | 17 | #include "kdevgocompletion_export.h" 18 | #include 19 | 20 | namespace go 21 | { 22 | 23 | class KDEVGOCOMPLETION_EXPORT TypeMatch 24 | { 25 | public: 26 | TypeMatch(); 27 | 28 | TypeMatch(const KDevelop::AbstractType::Ptr &m_singleType, const QList &m_multipleTypes); 29 | 30 | KDevelop::AbstractType::Ptr singleType() const; 31 | QList multipleTypes() const; 32 | 33 | void setSingleType(const KDevelop::AbstractType::Ptr &singleType); 34 | void setMultipleTypes(const QList &multipleTypes); 35 | 36 | private: 37 | KDevelop::AbstractType::Ptr m_singleType; 38 | QList m_multipleTypes; 39 | }; 40 | 41 | } 42 | 43 | #endif //GOLANGTYPEMATCH_H 44 | -------------------------------------------------------------------------------- /codecompletion/worker.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "worker.h" 20 | 21 | #include "context.h" 22 | #include "completiondebug.h" 23 | 24 | namespace go 25 | { 26 | CodeCompletionWorker::CodeCompletionWorker(KDevelop::CodeCompletionModel* model): KDevelop::CodeCompletionWorker(model) 27 | { 28 | 29 | } 30 | 31 | KDevelop::CodeCompletionContext* CodeCompletionWorker::createCompletionContext(const KDevelop::DUContextPointer& context, 32 | const QString& contextText, 33 | const QString& followingText, 34 | const KDevelop::CursorInRevision& position) const 35 | { 36 | Q_UNUSED(followingText); 37 | qCDebug(COMPLETION) << "Completion test"; 38 | //return go::CodeCompletionWorker::createCompletionContext(context, contextText, followingText, position); 39 | return new go::CodeCompletionContext(context, contextText, position); 40 | } 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /codecompletion/worker.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGCOMPLETIONWORKER_H 20 | #define GOLANGCOMPLETIONWORKER_H 21 | 22 | #include 23 | #include 24 | 25 | namespace go 26 | { 27 | class CodeCompletionWorker : public KDevelop::CodeCompletionWorker 28 | { 29 | public: 30 | CodeCompletionWorker(KDevelop::CodeCompletionModel* model); 31 | 32 | protected: 33 | KDevelop::CodeCompletionContext* createCompletionContext( 34 | const KDevelop::DUContextPointer& context, const QString& contextText, 35 | const QString& followingText, const KDevelop::CursorInRevision& position) const override; 36 | 37 | }; 38 | } 39 | #endif -------------------------------------------------------------------------------- /duchain/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | set(duchain_SRC 3 | builders/declarationbuilder.cpp 4 | builders/contextbuilder.cpp 5 | builders/typebuilder.cpp 6 | builders/usebuilder.cpp 7 | goducontext.cpp 8 | expressionvisitor.cpp 9 | helper.cpp 10 | duchaindebug.cpp 11 | 12 | types/gointegraltype.cpp 13 | types/gofunctiontype.cpp 14 | types/gostructuretype.cpp 15 | types/gomaptype.cpp 16 | types/gochantype.cpp 17 | declarations/functiondeclaration.cpp 18 | declarations/functiondefinition.cpp 19 | 20 | navigation/navigationwidget.cpp 21 | navigation/declarationnavigationcontext.cpp 22 | ) 23 | 24 | add_library( kdevgoduchain SHARED ${duchain_SRC}) 25 | generate_export_header(kdevgoduchain BASE_NAME kdevgoduchain EXPORT_MACRO_NAME KDEVGODUCHAIN_EXPORT) 26 | target_link_libraries(kdevgoduchain LINK_PRIVATE 27 | KDev::Interfaces 28 | KDev::Language 29 | KDev::Shell 30 | KDev::Project 31 | kdevgoparser 32 | ) 33 | 34 | install(TARGETS kdevgoduchain DESTINATION ${INSTALL_TARGETS_DEFAULT_ARGS}) 35 | 36 | if(BUILD_TESTING) 37 | add_subdirectory(tests) 38 | endif() 39 | -------------------------------------------------------------------------------- /duchain/builders/contextbuilder.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef KDEVGOLANGCONTEXTBUILDER_H 20 | #define KDEVGOLANGCONTEXTBUILDER_H 21 | 22 | #include 23 | 24 | #include "parser/goast.h" 25 | #include "parser/godefaultvisitor.h" 26 | #include "parser/parsesession.h" 27 | #include "duchain/kdevgoduchain_export.h" 28 | 29 | 30 | 31 | typedef KDevelop::AbstractContextBuilder ContextBuilderBase; 32 | 33 | class Editor 34 | { 35 | public: 36 | Editor(ParseSession** session) 37 | : m_session(session) 38 | {} 39 | 40 | ParseSession* parseSession() const 41 | { 42 | return *m_session; 43 | } 44 | private: 45 | ParseSession** m_session; 46 | }; 47 | 48 | 49 | class KDEVGODUCHAIN_EXPORT ContextBuilder: public ContextBuilderBase, public go::DefaultVisitor 50 | { 51 | public: 52 | ContextBuilder(); 53 | ~ContextBuilder() override; 54 | 55 | /*virtual KDevelop::ReferencedTopDUContext build(const KDevelop::IndexedString& url, go::AstNode* node, 56 | const KDevelop::ReferencedTopDUContext& updateContext 57 | = KDevelop::ReferencedTopDUContext());*/ 58 | 59 | void startVisiting(go::AstNode* node) override; 60 | void visitIfStmt(go::IfStmtAst* node) override; 61 | void visitBlock(go::BlockAst* node) override; 62 | 63 | /** 64 | * In go all imports must appear before first top level declaration. 65 | * This gives us the opportunity to call updateImportsCache() only once, when first 66 | * TopLevelDeclaration is encountered. 67 | **/ 68 | void visitTopLevelDeclaration(go::TopLevelDeclarationAst* node) override; 69 | void visitPrimaryExpr(go::PrimaryExprAst* node) override; 70 | 71 | KDevelop::DUContext* contextFromNode(go::AstNode* node) override; 72 | 73 | void setContextOnNode(go::AstNode* node, KDevelop::DUContext* context) override; 74 | 75 | KDevelop::RangeInRevision editorFindRange(go::AstNode* fromNode, go::AstNode* toNode) override; 76 | 77 | KDevelop::QualifiedIdentifier identifierForNode(go::IdentifierAst* node) override; 78 | 79 | KDevelop::QualifiedIdentifier identifierForIndex(qint64 index); 80 | 81 | void setParseSession(ParseSession* session); 82 | 83 | 84 | KDevelop::TopDUContext* newTopContext(const KDevelop::RangeInRevision& range, KDevelop::ParsingEnvironmentFile* file=0) override; 85 | 86 | KDevelop::DUContext* newContext(const KDevelop::RangeInRevision& range) override; 87 | 88 | 89 | KDevelop::QualifiedIdentifier createFullName(go::IdentifierAst* package, go::IdentifierAst* typeName); 90 | 91 | ParseSession* parseSession(); 92 | 93 | Editor* editor() const { return m_editor.data(); } 94 | 95 | /** 96 | * Extracts identifier from expression. 97 | * Grammar sometimes allows expressions where only identifiers should be allowed to simplify 98 | * parsing. This function extracts that identifiers. 99 | **/ 100 | go::IdentifierAst* identifierAstFromExpressionAst(go::ExpressionAst* node); 101 | 102 | protected: 103 | 104 | ParseSession* m_session; 105 | 106 | 107 | bool m_mapAst; // make KDevelop::AbstractContextBuilder happy 108 | QScopedPointer m_editor; // make KDevelop::AbstractUseBuilder happy 109 | bool m_expectMoreImports; 110 | 111 | }; 112 | 113 | #endif -------------------------------------------------------------------------------- /duchain/builders/declarationbuilder.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef KDEVGOLANGDECLBUILDER_H 20 | #define KDEVGOLANGDECLBUILDER_H 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "duchain/kdevgoduchain_export.h" 27 | #include "contextbuilder.h" 28 | #include "typebuilder.h" 29 | #include "parser/parsesession.h" 30 | #include "parser/goast.h" 31 | 32 | 33 | typedef KDevelop::AbstractDeclarationBuilder DeclarationBuilderBase; 34 | 35 | class KDEVGODUCHAIN_EXPORT DeclarationBuilder : public DeclarationBuilderBase 36 | { 37 | public: 38 | DeclarationBuilder(ParseSession* session, bool forExport); 39 | 40 | KDevelop::ReferencedTopDUContext build(const KDevelop::IndexedString& url, 41 | go::AstNode* node, 42 | const KDevelop::ReferencedTopDUContext& updateContext = KDevelop::ReferencedTopDUContext()) override; 43 | void startVisiting(go::AstNode* node) override; 44 | 45 | void visitVarSpec(go::VarSpecAst* node) override; 46 | void visitShortVarDecl(go::ShortVarDeclAst* node) override; 47 | void visitConstSpec(go::ConstSpecAst* node) override; 48 | void visitConstDecl(go::ConstDeclAst* node) override; 49 | void visitFuncDeclaration(go::FuncDeclarationAst* node) override; 50 | void visitMethodDeclaration(go::MethodDeclarationAst* node) override; 51 | void visitTypeSpec(go::TypeSpecAst* node) override; 52 | void visitImportSpec(go::ImportSpecAst* node) override; 53 | void visitSourceFile(go::SourceFileAst* node) override; 54 | void visitForStmt(go::ForStmtAst* node) override; 55 | void visitSwitchStmt(go::SwitchStmtAst* node) override; 56 | void visitTypeCaseClause(go::TypeCaseClauseAst* node) override; 57 | void visitExprCaseClause(go::ExprCaseClauseAst* node) override; 58 | void visitPrimaryExpr(go::PrimaryExprAst *node) override; 59 | 60 | /** 61 | * this handles variable declaration in select statements, e.g. 62 | * select { case i := <-mychan: bla bla... } 63 | * NOTE: right hand side expression must be a receive operator, returning two values */ 64 | void visitCommCase(go::CommCaseAst* node) override; 65 | void visitCommClause(go::CommClauseAst* node) override; 66 | 67 | void visitTypeDecl(go::TypeDeclAst* node) override; 68 | 69 | /** 70 | * A shortcut for ExpressionVisitor to build function type 71 | **/ 72 | go::GoFunctionDeclaration* buildFunction(go::SignatureAst* node, go::BlockAst* block = nullptr, go::IdentifierAst* name = nullptr, const QByteArray& comment = {}); 73 | 74 | go::GoFunctionDefinition* buildMethod(go::SignatureAst* node, go::BlockAst* block = nullptr, go::IdentifierAst* name = nullptr, 75 | go::GoFunctionDeclaration* pDeclaration = nullptr, const QByteArray &array = {}, const QualifiedIdentifier &identifier = {}); 76 | 77 | /*struct GoImport{ 78 | GoImport(bool anon, KDevelop::TopDUContext* ctx) : anonymous(anon), context(ctx) {} 79 | bool anonymous; 80 | KDevelop::TopDUContext* context; 81 | };*/ 82 | 83 | private: 84 | 85 | /** 86 | * Deduces types of expression with ExpressionVisitor and declares variables 87 | * from idList with respective types. If there is a single expression, returning multiple types 88 | * idList will get assigned those types. Otherwise we get only first type no matter how many of them 89 | * expression returns.(I believe this is how it works in Go, correct it if I'm wrong) 90 | * @param declareConstant whether to declare usual variables or constants 91 | */ 92 | void declareVariables(go::IdentifierAst* id, go::IdListAst* idList, go::ExpressionAst* expression, 93 | go::ExpressionListAst* expressionList, bool declareConstant); 94 | /** 95 | * declares variables or constants with names from id and idList of type type. 96 | */ 97 | void declareVariablesWithType(go::IdentifierAst* id, go::IdListAst* idList, go::TypeAst* type, bool declareConstant); 98 | 99 | /** 100 | * Declares variable with identifier @param id of type @param type 101 | **/ 102 | void declareVariable(go::IdentifierAst* id, const AbstractType::Ptr& type) override; 103 | 104 | /** 105 | * Declares GoFunction and assigns contexts to it. 106 | * Called from typebuilder when building functions and methods 107 | **/ 108 | go::GoFunctionDeclaration* declareFunction(go::IdentifierAst* id, const go::GoFunctionType::Ptr& type, 109 | DUContext* paramContext, DUContext* retparamContext, const QByteArray& comment = {}, DUContext* bodyContext = nullptr) override; 110 | 111 | go::GoFunctionDefinition* declareMethod(go::IdentifierAst* id, const go::GoFunctionType::Ptr& type, 112 | DUContext* paramContext, DUContext* retparamContext, const QByteArray& comment=QByteArray(), 113 | DUContext* bodyContext = nullptr, go::GoFunctionDeclaration* declaration = nullptr, const QualifiedIdentifier &identifier = {}); 114 | 115 | void importThisPackage(); 116 | void importBuiltins(); 117 | bool m_export; 118 | 119 | //QHash m_anonymous_imports; 120 | 121 | bool m_preBuilding; 122 | QList m_constAutoTypes; 123 | QualifiedIdentifier m_thisPackage; 124 | QualifiedIdentifier m_switchTypeVariable; 125 | QByteArray m_lastTypeComment, m_lastConstComment; 126 | }; 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /duchain/builders/typebuilder.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef TYPEBUILDER_H 20 | #define TYPEBUILDER_H 21 | 22 | #include 23 | #include 24 | #include 25 | #include "contextbuilder.h" 26 | #include "duchain/kdevgoduchain_export.h" 27 | #include "duchain/declarations/functiondeclaration.h" 28 | #include "duchain/types/gofunctiontype.h" 29 | 30 | namespace go 31 | { 32 | 33 | typedef KDevelop::AbstractTypeBuilder TypeBuilderBase; 34 | 35 | class KDEVGODUCHAIN_EXPORT TypeBuilder : public TypeBuilderBase 36 | { 37 | public: 38 | void visitTypeName(go::TypeNameAst* node) override; 39 | void visitArrayOrSliceType(go::ArrayOrSliceTypeAst* node) override; 40 | void visitPointerType(go::PointerTypeAst* node) override; 41 | void visitStructType(go::StructTypeAst* node) override; 42 | void visitFieldDecl(go::FieldDeclAst* node) override; 43 | void visitInterfaceType(go::InterfaceTypeAst* node) override; 44 | void visitMethodSpec(go::MethodSpecAst* node) override; 45 | void visitMapType(go::MapTypeAst* node) override; 46 | void visitChanType(go::ChanTypeAst* node) override; 47 | void visitFunctionType(go::FunctionTypeAst* node) override; 48 | void visitParameter(go::ParameterAst* node) override; 49 | 50 | /** 51 | * When building named types we often have IdentifierAst instead of TypeNameAst, 52 | * so it makes sense to have this convenience function 53 | **/ 54 | void buildTypeName(go::IdentifierAst* typeName, go::IdentifierAst* fullName = 0); 55 | 56 | /** 57 | * Used by external classes like ExpressionVisitor after building a type. 58 | */ 59 | AbstractType::Ptr getLastType() { return lastType(); } 60 | 61 | protected: 62 | 63 | //when building some types we need to open declarations 64 | //so next methods are placeholders for that, which will be implemented in DeclarationBuilder 65 | //that way we can keep type building logic in TypeBuilder 66 | 67 | /** 68 | * declared here as pure virtual so we can use that when building functions, structs and interfaces. 69 | **/ 70 | virtual void declareVariable(go::IdentifierAst* id, const KDevelop::AbstractType::Ptr& type) = 0; 71 | 72 | /** 73 | * declared here as pure virtual so we can use that when building functions 74 | **/ 75 | virtual go::GoFunctionDeclaration* declareFunction(go::IdentifierAst* id, const GoFunctionType::Ptr& type, 76 | DUContext* paramContext, DUContext* retparamContext, 77 | const QByteArray& comment = {}, DUContext* bodyContext = nullptr) = 0; 78 | 79 | /** 80 | * opens GoFunctionType, parses it's parameters and return declaration if @param declareParameters is true. 81 | **/ 82 | go::GoFunctionType::Ptr parseSignature(go::SignatureAst *node, bool declareParameters, DUContext **parametersContext = nullptr, 83 | DUContext **returnArgsContext = nullptr, const QualifiedIdentifier &identifier = {}, 84 | const QByteArray &comment = {}); 85 | 86 | /** 87 | * Convenience function that parses function parameters. 88 | * @param parseParemeters if true - add parameter to function arguments, otherwise add it to return params 89 | * @param declareParameters open parameter declarations if true 90 | **/ 91 | void parseParameters(go::ParametersAst* node, bool parseParameters=true, bool declareParameters=false); 92 | 93 | /** 94 | * Convenience function that adds argument to function params or output params 95 | **/ 96 | void addArgumentHelper(go::GoFunctionType::Ptr function, KDevelop::AbstractType::Ptr argument, bool parseArguments); 97 | 98 | KDevelop::QualifiedIdentifier m_contextIdentifier; 99 | 100 | }; 101 | 102 | } 103 | 104 | #endif -------------------------------------------------------------------------------- /duchain/builders/usebuilder.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGUSEBUILDER_H 20 | #define GOLANGUSEBUILDER_H 21 | 22 | #include 23 | 24 | #include "parser/goast.h" 25 | #include "contextbuilder.h" 26 | 27 | #include 28 | 29 | namespace go 30 | { 31 | 32 | typedef KDevelop::AbstractUseBuilder UseBuilderBase; 33 | 34 | class KDEVGODUCHAIN_EXPORT UseBuilder : public UseBuilderBase 35 | { 36 | public: 37 | UseBuilder(ParseSession* session); 38 | 39 | 40 | //virtual KDevelop::ReferencedTopDUContext build(const KDevelop::IndexedString& url, AstNode* node, 41 | //KDevelop::ReferencedTopDUContext updateContext = KDevelop::ReferencedTopDUContext()); 42 | 43 | void visitPrimaryExpr(go::PrimaryExprAst* node) override; 44 | void visitTypeName(go::TypeNameAst* node) override; 45 | void visitBlock(go::BlockAst* node) override; 46 | void visitMethodDeclaration(go::MethodDeclarationAst* node) override; 47 | void visitShortVarDecl(go::ShortVarDeclAst* node) override; 48 | void visitLiteralValue(go::LiteralValueAst* node) override; 49 | 50 | private: 51 | void createUseInDeclaration(IdentifierAst *idNode); 52 | void createUseForField(const ElementAst *fieldElement, const KDevelop::QualifiedIdentifier &typeId, KDevelop::DUContext *context); 53 | 54 | QStack m_types; 55 | }; 56 | 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /duchain/declarations/functiondeclaration.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "functiondeclaration.h" 20 | #include 21 | 22 | using namespace KDevelop; 23 | 24 | namespace go { 25 | 26 | REGISTER_DUCHAIN_ITEM(GoFunctionDeclaration); 27 | 28 | GoFunctionDeclaration::GoFunctionDeclaration(const go::GoFunctionDeclaration& rhs): 29 | FunctionDeclaration(*new GoFunctionDeclarationData(*rhs.d_func())) 30 | { 31 | 32 | } 33 | 34 | GoFunctionDeclaration::GoFunctionDeclaration(const RangeInRevision& range, DUContext* context): 35 | FunctionDeclaration(*new GoFunctionDeclarationData, range) 36 | { 37 | d_func_dynamic()->setClassId(this); 38 | if (context) { 39 | setContext(context); 40 | } 41 | } 42 | 43 | GoFunctionDeclaration::GoFunctionDeclaration(go::GoFunctionDeclarationData& data): FunctionDeclaration(data) 44 | { 45 | 46 | } 47 | 48 | GoFunctionDeclaration::GoFunctionDeclaration(go::GoFunctionDeclarationData& data, const RangeInRevision& range): 49 | FunctionDeclaration(data, range) 50 | { 51 | 52 | } 53 | 54 | Declaration* GoFunctionDeclaration::clonePrivate() const 55 | { 56 | return new GoFunctionDeclaration(*this); 57 | } 58 | 59 | 60 | QString GoFunctionDeclaration::toString() const 61 | { 62 | return KDevelop::FunctionDeclaration::toString(); 63 | //return QString("test func declaration"); 64 | } 65 | 66 | void GoFunctionDeclaration::setReturnArgsContext(DUContext* context) 67 | { 68 | d_func_dynamic()->returnContext = context; 69 | } 70 | 71 | DUContext* GoFunctionDeclaration::returnArgsContext() const 72 | { 73 | return d_func()->returnContext.context(); 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /duchain/declarations/functiondeclaration.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGFUNCDECL_H 20 | #define GOLANGFUNCDECL_H 21 | 22 | #include 23 | 24 | #include "kdevgoduchain_export.h" 25 | 26 | namespace go { 27 | 28 | class GoFunctionDeclarationData : public KDevelop::FunctionDeclarationData 29 | { 30 | public: 31 | GoFunctionDeclarationData() : KDevelop::FunctionDeclarationData() 32 | { 33 | } 34 | 35 | GoFunctionDeclarationData(const GoFunctionDeclarationData& rhs) : KDevelop::FunctionDeclarationData(rhs), returnContext(rhs.returnContext) 36 | { 37 | } 38 | 39 | KDevelop::IndexedDUContext returnContext; 40 | }; 41 | 42 | //typedef KDevelop::FunctionDeclarationData GoFunctionDeclarationData; 43 | 44 | 45 | class KDEVGODUCHAIN_EXPORT GoFunctionDeclaration : public KDevelop::FunctionDeclaration 46 | { 47 | public: 48 | 49 | GoFunctionDeclaration(const GoFunctionDeclaration& rhs); 50 | GoFunctionDeclaration(const KDevelop::RangeInRevision& range, KDevelop::DUContext* context); 51 | GoFunctionDeclaration(GoFunctionDeclarationData& data); 52 | GoFunctionDeclaration(GoFunctionDeclarationData& data, const KDevelop::RangeInRevision& range); 53 | 54 | 55 | QString toString() const override; 56 | 57 | void setReturnArgsContext(KDevelop::DUContext* context); 58 | 59 | KDevelop::DUContext* returnArgsContext() const; 60 | 61 | enum { 62 | Identity = 121 63 | }; 64 | 65 | KDevelop::Declaration* clonePrivate() const override; 66 | 67 | private: 68 | DUCHAIN_DECLARE_DATA(GoFunctionDeclaration); 69 | 70 | }; 71 | 72 | } 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /duchain/declarations/functiondefinition.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "functiondefinition.h" 12 | #include 13 | 14 | using namespace KDevelop; 15 | 16 | namespace go { 17 | 18 | REGISTER_DUCHAIN_ITEM(GoFunctionDefinition); 19 | 20 | GoFunctionDefinition::GoFunctionDefinition(const KDevelop::RangeInRevision& range, KDevelop::DUContext* context): 21 | FunctionDefinition(*new GoFunctionDefinitionData) 22 | { 23 | setRange(range); 24 | d_func_dynamic()->setClassId(this); 25 | if (context) { 26 | setContext(context); 27 | } 28 | } 29 | 30 | GoFunctionDefinition::GoFunctionDefinition(GoFunctionDefinitionData& data) : FunctionDefinition(data) 31 | { 32 | 33 | } 34 | 35 | void GoFunctionDefinition::setReturnArgsContext(KDevelop::DUContext* context) 36 | { 37 | d_func_dynamic()->returnContext = context; 38 | } 39 | 40 | 41 | DUContext* GoFunctionDefinition::returnArgsContext() const 42 | { 43 | return d_func()->returnContext.context(); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /duchain/declarations/functiondefinition.h: -------------------------------------------------------------------------------- 1 | /* KDevelop go build support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef GOLANGFUNCTIONDEFINITION_H 12 | #define GOLANGFUNCTIONDEFINITION_H 13 | 14 | #include 15 | 16 | #include "kdevgoduchain_export.h" 17 | 18 | namespace go { 19 | 20 | class GoFunctionDefinitionData : public KDevelop::FunctionDefinitionData 21 | { 22 | public: 23 | GoFunctionDefinitionData() : KDevelop::FunctionDefinitionData() 24 | { 25 | } 26 | 27 | GoFunctionDefinitionData(const GoFunctionDefinitionData& rhs) : KDevelop::FunctionDefinitionData(rhs), returnContext(rhs.returnContext) 28 | { 29 | } 30 | 31 | KDevelop::IndexedDUContext returnContext; 32 | }; 33 | 34 | class KDEVGODUCHAIN_EXPORT GoFunctionDefinition : public KDevelop::FunctionDefinition 35 | { 36 | public: 37 | GoFunctionDefinition(const KDevelop::RangeInRevision& range, KDevelop::DUContext* context); 38 | GoFunctionDefinition(GoFunctionDefinitionData& data); 39 | 40 | void setReturnArgsContext(KDevelop::DUContext* context); 41 | 42 | KDevelop::DUContext* returnArgsContext() const; 43 | 44 | enum { 45 | Identity = 122 46 | }; 47 | private: 48 | DUCHAIN_DECLARE_DATA(GoFunctionDefinition); 49 | 50 | }; 51 | 52 | } 53 | 54 | #endif -------------------------------------------------------------------------------- /duchain/duchaindebug.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "duchaindebug.h" 20 | 21 | Q_LOGGING_CATEGORY(DUCHAIN, "kdev-go-duchain") -------------------------------------------------------------------------------- /duchain/duchaindebug.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GODUCHAINDEBUG_H 20 | #define GODUCHAINDEBUG_H 21 | 22 | #include 23 | Q_DECLARE_LOGGING_CATEGORY(DUCHAIN) 24 | 25 | #endif -------------------------------------------------------------------------------- /duchain/expressionvisitor.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGEXPRVISITOR_H 20 | #define GOLANGEXPRVISITOR_H 21 | 22 | #include "parser/godefaultvisitor.h" 23 | #include "parser/goast.h" 24 | #include "builders/declarationbuilder.h" 25 | #include "kdevgoduchain_export.h" 26 | 27 | namespace go 28 | { 29 | 30 | class KDEVGODUCHAIN_EXPORT ExpressionVisitor : public DefaultVisitor 31 | { 32 | public: 33 | ExpressionVisitor(ParseSession* session, KDevelop::DUContext* context, DeclarationBuilder* builder=0); 34 | 35 | QList lastTypes(); 36 | 37 | void visitExpression(go::ExpressionAst* node) override; 38 | void visitUnaryExpression(go::UnaryExpressionAst* node) override; 39 | void visitPrimaryExpr(go::PrimaryExprAst* node) override; 40 | void visitBasicLit(go::BasicLitAst* node) override; 41 | void visitPrimaryExprResolve(go::PrimaryExprResolveAst* node) override; 42 | void visitCallOrBuiltinParam(go::CallOrBuiltinParamAst* node) override; 43 | void visitCallParam(go::CallParamAst* node) override; 44 | void visitTypeName(go::TypeNameAst* node) override; 45 | void visitStructType(go::StructTypeAst* node) override; 46 | void visitMapType(go::MapTypeAst* node) override; 47 | void visitPointerType(go::PointerTypeAst* node) override; 48 | void visitInterfaceType(go::InterfaceTypeAst* node) override; 49 | void visitChanType(go::ChanTypeAst* node) override; 50 | void visitParenType(go::ParenTypeAst* node) override; 51 | 52 | void visitBlock(go::BlockAst* node) override; 53 | /** 54 | * Visits range expression. 55 | * There isn't an actual range expression rule in grammar, so you have to manually 56 | * find range token and call this function on subsequent expression. 57 | **/ 58 | void visitRangeClause(go::ExpressionAst* node); 59 | 60 | /** 61 | * Delete all stored types and uses 62 | */ 63 | void clearAll(); 64 | 65 | QList allDeclarations(); 66 | QList allIds(); 67 | 68 | /** 69 | * TODO Currently only codecompletion needs declarations 70 | **/ 71 | DeclarationPointer lastDeclaration() { return m_declaration; } 72 | 73 | private: 74 | void pushType(AbstractType::Ptr type); 75 | 76 | QList popTypes(); 77 | 78 | void addType(AbstractType::Ptr type); 79 | 80 | void pushUse(IdentifierAst* node, Declaration* declaration); 81 | 82 | AbstractType::Ptr resolveTypeAlias(AbstractType::Ptr type); 83 | 84 | QualifiedIdentifier identifierForNode(IdentifierAst* node); 85 | 86 | /** 87 | * Converts complex type literals or conversions (imp.imptype{} or imp.imptype(1)) 88 | * to identified types. 89 | **/ 90 | bool handleComplexLiteralsAndConversions(PrimaryExprResolveAst* node, Declaration* decl); 91 | 92 | /** 93 | * Handle 'make', 'append' and 'new' methods since they don't follow go syntax 94 | * and cannot be handled by builtins.go 95 | **/ 96 | bool handleBuiltinFunction(PrimaryExprAst* node); 97 | 98 | /** 99 | * Handles stuff like basic literals(3.1415, 'strings', ...), type conversions( interface{}(variable) ... ), 100 | * anonymous functions ( func() { callMe(); } ) and so on. 101 | **/ 102 | void handleLiteralsAndConversions(PrimaryExprAst* node); 103 | 104 | ParseSession* m_session; 105 | DUContext* m_context; 106 | DeclarationBuilder* m_builder; 107 | //QList m_types; 108 | 109 | private: 110 | QList m_ids; 111 | QList m_declarations; 112 | QList m_types; 113 | DeclarationPointer m_declaration; 114 | 115 | }; 116 | 117 | } 118 | #endif -------------------------------------------------------------------------------- /duchain/goducontext.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "goducontext.h" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "navigation/navigationwidget.h" 27 | #include "duchaindebug.h" 28 | 29 | 30 | using namespace KDevelop; 31 | 32 | namespace go { 33 | 34 | typedef GoDUContext GoTopDUContext; 35 | REGISTER_DUCHAIN_ITEM_WITH_DATA(GoTopDUContext, TopDUContextData); 36 | 37 | typedef GoDUContext GoNormalDUContext; 38 | REGISTER_DUCHAIN_ITEM_WITH_DATA(GoNormalDUContext, DUContextData); 39 | 40 | template <> 41 | AbstractNavigationWidget* 42 | GoTopDUContext::createNavigationWidget(Declaration* decl, TopDUContext* topContext, 43 | AbstractNavigationWidget::DisplayHints hints) const { 44 | if (!decl) { 45 | qCDebug(DUCHAIN) << "no declaration, not returning navigationwidget"; 46 | return 0; 47 | } 48 | return new NavigationWidget(decl, topContext, hints); 49 | } 50 | 51 | template <> 52 | AbstractNavigationWidget* 53 | GoNormalDUContext::createNavigationWidget(Declaration* decl, TopDUContext* topContext, 54 | AbstractNavigationWidget::DisplayHints hints) const { 55 | if (!decl) { 56 | qCDebug(DUCHAIN) << "no declaration, not returning navigationwidget"; 57 | return 0; 58 | } 59 | return new NavigationWidget(decl, topContext, hints); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /duchain/goducontext.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGDUCONTEXT_H 20 | #define GOLANGDUCONTEXT_H 21 | 22 | #include 23 | 24 | class QWidget; 25 | 26 | namespace KDevelop 27 | { 28 | class Declaration; 29 | class TopDUContext; 30 | } 31 | 32 | namespace go { 33 | 34 | template 35 | class GoDUContext : public BaseContext 36 | { 37 | public: 38 | 39 | template 40 | GoDUContext(Data& data) : BaseContext(data) { 41 | } 42 | 43 | ///Parameters will be reached to the base-class 44 | template 45 | GoDUContext(Params... params) : BaseContext(params...) { 46 | static_cast(this)->d_func_dynamic()->setClassId(this); 47 | } 48 | 49 | KDevelop::AbstractNavigationWidget* 50 | createNavigationWidget(KDevelop::Declaration* decl, KDevelop::TopDUContext* topContext, 51 | KDevelop::AbstractNavigationWidget::DisplayHints hints) const override; 52 | 53 | enum { 54 | Identity = BaseContext::Identity + 51 55 | }; 56 | 57 | }; 58 | 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /duchain/helper.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGHELPER_H 20 | #define GOLANGHELPER_H 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "kdevgoduchain_export.h" 27 | 28 | using namespace KDevelop; 29 | 30 | namespace go 31 | { 32 | 33 | class IdentifierAst; 34 | class MethodRecvAst; 35 | 36 | class KDEVGODUCHAIN_EXPORT Helper 37 | { 38 | public: 39 | static QList getSearchPaths(QUrl document=QUrl()); 40 | static QString getBuiltinFile(); 41 | static ReferencedTopDUContext getBuiltinContext(); 42 | private: 43 | static QList m_CachedSearchPaths; 44 | static QString builtinFile; 45 | static DUChainPointer builtinContext; 46 | static QByteArray getGoEnv(QString name); 47 | }; 48 | 49 | KDEVGODUCHAIN_EXPORT DeclarationPointer getDeclaration(QualifiedIdentifier id, DUContext* context, bool searchInParent=true); 50 | 51 | KDEVGODUCHAIN_EXPORT go::IdentifierAst* getMethodRecvTypeIdentifier(go::MethodRecvAst* methodRecv); 52 | 53 | /** 54 | * This tries to find declaration which has a real type, like Instance and Type 55 | * but skips declarations like Namespace, NamespaceAlias and Import which can be 56 | * packages or type methods(but not the actual type declarations) 57 | */ 58 | KDEVGODUCHAIN_EXPORT DeclarationPointer getTypeOrVarDeclaration(QualifiedIdentifier id, DUContext* context, bool searchInParent=true); 59 | 60 | /** 61 | * This only looks for type declarations 62 | */ 63 | KDEVGODUCHAIN_EXPORT DeclarationPointer getTypeDeclaration(QualifiedIdentifier id, DUContext* context, bool searchInParent=true); 64 | 65 | KDEVGODUCHAIN_EXPORT QList getDeclarations(QualifiedIdentifier id, DUContext* context, bool searchInParent=true); 66 | 67 | 68 | KDEVGODUCHAIN_EXPORT DeclarationPointer getFirstDeclaration(DUContext* context, bool searchInParent=true); 69 | 70 | /** 71 | * Checks if topContext declares package @param id 72 | */ 73 | KDEVGODUCHAIN_EXPORT DeclarationPointer checkPackageDeclaration(Identifier id, TopDUContext* context); 74 | 75 | } 76 | 77 | #endif -------------------------------------------------------------------------------- /duchain/navigation/declarationnavigationcontext.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGDECLNAVCONTEXT_H 20 | #define GOLANGDECLNAVCONTEXT_H 21 | 22 | #include 23 | #include 24 | 25 | 26 | class DeclarationNavigationContext : public KDevelop::AbstractDeclarationNavigationContext 27 | { 28 | public: 29 | DeclarationNavigationContext(KDevelop::DeclarationPointer decl, 30 | KDevelop::TopDUContextPointer topContext, 31 | KDevelop::AbstractNavigationContext* previousContext = 0); 32 | protected: 33 | void htmlFunction() override; 34 | 35 | void eventuallyMakeTypeLinks( KDevelop::AbstractType::Ptr type ) override; 36 | 37 | QString html(bool shorten) override; 38 | }; 39 | 40 | 41 | #endif -------------------------------------------------------------------------------- /duchain/navigation/navigationwidget.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "navigation/navigationwidget.h" 20 | 21 | #include "navigation/declarationnavigationcontext.h" 22 | #include 23 | 24 | using namespace KDevelop; 25 | 26 | NavigationWidget::NavigationWidget(KDevelop::Declaration* decl, KDevelop::TopDUContext* topContext, 27 | const KDevelop::AbstractNavigationWidget::DisplayHints hints) 28 | { 29 | auto context = NavigationContextPointer(new DeclarationNavigationContext(DeclarationPointer(decl), TopDUContextPointer(topContext), nullptr)); 30 | 31 | setDisplayHints(hints); 32 | setContext(context); 33 | } 34 | -------------------------------------------------------------------------------- /duchain/navigation/navigationwidget.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGNAVWIDGET_H 20 | #define GOLANGNAVWIDGET_H 21 | 22 | #include "kdevgoduchain_export.h" 23 | 24 | #include 25 | 26 | class KDEVGODUCHAIN_EXPORT NavigationWidget : public KDevelop::AbstractNavigationWidget 27 | { 28 | public: 29 | NavigationWidget(KDevelop::Declaration* decl, KDevelop::TopDUContext* topContext, 30 | const KDevelop::AbstractNavigationWidget::DisplayHints hints); 31 | 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /duchain/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ecm_add_test(testduchain.cpp 2 | TEST_NAME duchain 3 | LINK_LIBRARIES 4 | Qt5::Test 5 | KDev::Language 6 | KDev::Tests 7 | kdevgoduchain 8 | kdevgoparser 9 | ) 10 | -------------------------------------------------------------------------------- /duchain/tests/testduchain.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGTESTDUCHAIN_H 20 | #define GOLANGTESTDUCHAIN_H 21 | 22 | #include 23 | 24 | class TestDuchain : public QObject 25 | { 26 | Q_OBJECT 27 | private slots: 28 | void initTestCase(); 29 | void sanityCheck(); 30 | void cleanupTestCase(); 31 | void builtinFunctions_data(); 32 | void builtinFunctions(); 33 | void test_declareVariables(); 34 | void test_redeclareVariables_data(); 35 | void test_redeclareVariables(); 36 | void test_constants(); 37 | void test_constants_omittedType(); 38 | void test_indexexpressions_data(); 39 | void test_indexexpressions(); 40 | void test_ifcontexts(); 41 | void test_funccontexts(); 42 | void test_rangeclause_data(); 43 | void test_rangeclause(); 44 | void test_typeswitch(); 45 | void test_funcparams_data(); 46 | void test_funcparams(); 47 | void test_literals_data(); 48 | void test_literals(); 49 | void test_unaryOps_data(); 50 | void test_unaryOps(); 51 | void test_typeAssertions(); 52 | void test_selectCases(); 53 | void test_declareVariablesInParametersOfNestedFunction(); 54 | void test_usesAreAddedInCorrectContext(); 55 | void test_usesAreCreatedInPlaceOfStructFields(); 56 | void test_functionContextIsCreatedWhenDeclaringAsMemberOfStruct_data(); 57 | void test_functionContextIsCreatedWhenDeclaringAsMemberOfStruct(); 58 | }; 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /duchain/types/gochantype.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "gochantype.h" 20 | 21 | #include 22 | 23 | using namespace KDevelop; 24 | 25 | namespace go 26 | { 27 | REGISTER_TYPE(GoChanType); 28 | 29 | GoChanType::GoChanType(const GoChanType& rhs) 30 | : KDevelop::AbstractType(copyData(*rhs.d_func())) 31 | { 32 | } 33 | 34 | GoChanType::GoChanType(GoChanTypeData& data) 35 | : KDevelop::AbstractType(data) 36 | { 37 | } 38 | 39 | GoChanType::GoChanType() 40 | : KDevelop::AbstractType(createData()) 41 | { 42 | } 43 | 44 | void GoChanType::setValueType(AbstractType::Ptr type) 45 | { 46 | d_func_dynamic()->valueType = type->indexed(); 47 | } 48 | 49 | void GoChanType::setKind(uint kind) 50 | { 51 | d_func_dynamic()->kind = kind; 52 | } 53 | 54 | AbstractType::Ptr GoChanType::valueType() 55 | { 56 | return d_func()->valueType.abstractType(); 57 | } 58 | 59 | uint GoChanType::kind() 60 | { 61 | return d_func()->kind; 62 | } 63 | 64 | QString GoChanType::toString() const 65 | { 66 | QString typeName; 67 | if(d_func()->kind == ChanKind::Receive) 68 | typeName = "<- chan"; 69 | else if(d_func()->kind == ChanKind::Send) 70 | typeName = "chan <-"; 71 | else 72 | typeName = "chan"; 73 | typeName = typeName + " "+d_func()->valueType.abstractType()->toString(); 74 | return typeName; 75 | } 76 | 77 | KDevelop::AbstractType* GoChanType::clone() const 78 | { 79 | return new GoChanType(*this); 80 | } 81 | 82 | uint GoChanType::hash() const 83 | { 84 | uint hash = 6 * KDevelop::AbstractType::hash(); 85 | hash += 6 * d_func()->valueType.hash(); 86 | hash += 6 * d_func()->kind; 87 | return hash; 88 | } 89 | 90 | void GoChanType::accept0(TypeVisitor *v) const 91 | { 92 | v->visit(this); 93 | } 94 | 95 | bool GoChanType::equals(const AbstractType* rhs) const 96 | { 97 | if(this == rhs) 98 | return true; 99 | 100 | if(!AbstractType::equals(rhs)) 101 | return false; 102 | 103 | Q_ASSERT( fastCast(rhs) ); 104 | 105 | const GoChanType* type = static_cast(rhs); 106 | 107 | return d_func()->valueType == type->d_func()->valueType && d_func()->kind == type->d_func()->kind; 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /duchain/types/gochantype.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGCHANTYPE_H 20 | #define GOLANGCHANTYPE_H 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "duchain/kdevgoduchain_export.h" 27 | 28 | namespace go 29 | { 30 | 31 | class GoChanTypeData : public KDevelop::AbstractTypeData 32 | { 33 | public: 34 | GoChanTypeData() 35 | : KDevelop::AbstractTypeData() 36 | { 37 | kind = 0; 38 | } 39 | GoChanTypeData( const GoChanTypeData& rhs ) 40 | : KDevelop::AbstractTypeData(rhs), valueType(rhs.valueType), kind(rhs.kind) 41 | { 42 | } 43 | 44 | KDevelop::IndexedType valueType; 45 | uint kind; //channel kind(0 = "chan", 1 = "<-chan", 2 = "chan <-") 46 | }; 47 | 48 | class KDEVGODUCHAIN_EXPORT GoChanType: public KDevelop::AbstractType 49 | { 50 | public: 51 | typedef KDevelop::TypePtr Ptr; 52 | 53 | /// Default constructor 54 | GoChanType(); 55 | /// Copy constructor. \param rhs type to copy 56 | GoChanType(const GoChanType& rhs); 57 | /// Constructor using raw data. \param data internal data. 58 | GoChanType(GoChanTypeData& data); 59 | 60 | void setValueType(AbstractType::Ptr type); 61 | 62 | void setKind(uint kind); 63 | 64 | AbstractType::Ptr valueType(); 65 | 66 | uint kind(); 67 | 68 | QString toString() const override; 69 | 70 | KDevelop::AbstractType* clone() const override; 71 | 72 | uint hash() const override; 73 | 74 | bool equals(const AbstractType* rhs) const override; 75 | 76 | void accept0(KDevelop::TypeVisitor *v) const override; 77 | 78 | enum ChanKind{ 79 | SendAndReceive=0, 80 | Receive=1, 81 | Send=2 82 | }; 83 | 84 | enum { 85 | Identity = 106 86 | }; 87 | 88 | typedef GoChanTypeData Data; 89 | typedef KDevelop::AbstractType BaseType; 90 | 91 | protected: 92 | TYPE_DECLARE_DATA(GoChanType); 93 | }; 94 | 95 | } 96 | 97 | namespace KDevelop 98 | { 99 | 100 | template<> 101 | inline go::GoChanType* fastCast(AbstractType* from) { 102 | if ( !from || from->whichType() != AbstractType::TypeAbstract ) { 103 | return 0; 104 | } else { 105 | return dynamic_cast(from); 106 | } 107 | } 108 | 109 | } 110 | 111 | 112 | #endif -------------------------------------------------------------------------------- /duchain/types/gofunctiontype.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "gofunctiontype.h" 20 | 21 | #include 22 | 23 | using namespace KDevelop; 24 | 25 | namespace go 26 | { 27 | 28 | REGISTER_TYPE(GoFunctionType); 29 | 30 | DEFINE_LIST_MEMBER_HASH(GoFunctionTypeData, m_returnArgs, IndexedType) 31 | 32 | GoFunctionType::GoFunctionType(GoFunctionTypeData& data) 33 | : FunctionType(data) 34 | { 35 | } 36 | 37 | GoFunctionType::GoFunctionType(const GoFunctionType& rhs) 38 | : FunctionType(copyData(*rhs.d_func())) 39 | { 40 | } 41 | 42 | GoFunctionType::GoFunctionType() 43 | : FunctionType(createData()) 44 | { 45 | } 46 | 47 | //TODO cleanup this code 48 | 49 | void GoFunctionType::addReturnArgument(AbstractType::Ptr arg) 50 | { 51 | Q_ASSERT(arg); 52 | d_func_dynamic()->m_returnArgsList().append(arg->indexed()); 53 | //qCDebug(DUCHAIN) << " size check: "<< d_func()->m_returnArgs.size(); 54 | //makeDynamic(); 55 | //qCDebug(DUCHAIN) << "size check: " << d_func()->m_returnArguments.size(); 56 | } 57 | 58 | QList GoFunctionType::returnArguments() const 59 | { 60 | QList ret; 61 | FOREACH_FUNCTION(const IndexedType& arg , d_func()->m_returnArgs) 62 | ret << arg.abstractType(); 63 | return ret; 64 | //QList list; 65 | //for(const AbstractType::Ptr& type : d_func()->m_returnArguments) 66 | //list.append(type); 67 | //return list; 68 | } 69 | 70 | QString GoFunctionType::toString() const 71 | { 72 | //return FunctionType::toString(); 73 | QString output = "function ("; 74 | auto args = arguments(); 75 | for(const AbstractType::Ptr& type : args) 76 | { 77 | output = output.append(!type ? "" : type->toString()); 78 | if(args.back() != type) 79 | output = output.append(", "); 80 | } 81 | output = output.append(") "); 82 | //auto rargs = d_func()->m_returnArgs; 83 | auto rargs = returnArguments(); 84 | //qCDebug(DUCHAIN) << "size check again: " << d_func()->m_returnArgs.size(); 85 | if(rargs.size() == 0) 86 | return output; 87 | if(rargs.size() == 1) 88 | { 89 | if(!rargs.first()) 90 | return output.append(""); 91 | //qCDebug(DUCHAIN) << rargs.first().abstractType()->toString(); 92 | return output.append(rargs.first()->toString()); 93 | } 94 | 95 | output = output.append("("); 96 | foreach(const AbstractType::Ptr& type, rargs) 97 | { 98 | //auto type = idx.abstractType(); 99 | output = output.append(!type ? "" : type->toString()); 100 | if(rargs.back() != type) 101 | output = output.append(", "); 102 | } 103 | return output.append(")"); 104 | } 105 | 106 | KDevelop::AbstractType* GoFunctionType::clone() const 107 | { 108 | return new GoFunctionType(*this); 109 | } 110 | 111 | uint GoFunctionType::hash() const 112 | { 113 | /* KDevHash hash(6 * KDevelop::FunctionType::hash()); 114 | //for(const IndexedType& idx : d_func()->m_returnArgsList()) 115 | FOREACH_FUNCTION(const IndexedType& idx, d_func()->m_returnArgs) 116 | hash << idx.hash(); 117 | return hash;*/ 118 | 119 | 120 | uint h = 6 * FunctionType::hash(); 121 | //for ( uint i = 0; i < d_func()->m_returnArgsSize(); i++ ) { 122 | FOREACH_FUNCTION(const IndexedType& idx, d_func()->m_returnArgs) { 123 | //h += d_func()->m_returnArgs()[i].hash(); 124 | h += idx.hash(); 125 | } 126 | return h; 127 | } 128 | 129 | bool GoFunctionType::equals(const AbstractType* rhs) const 130 | { 131 | if ( this == rhs ) { 132 | return true; 133 | } 134 | if ( ! KDevelop::FunctionType::equals(rhs) ) { 135 | return false; 136 | } 137 | Q_ASSERT(fastCast(rhs) ); 138 | const GoFunctionType* _rhs = static_cast(rhs); 139 | TYPE_D(GoFunctionType); 140 | 141 | if ( d->m_returnArgsSize() != _rhs->d_func()->m_returnArgsSize()) 142 | return false; 143 | 144 | for(unsigned int a = 0; a < d->m_returnArgsSize(); ++a) 145 | if(d->m_returnArgs()[a] != _rhs->d_func()->m_returnArgs()[a]) 146 | return false; 147 | 148 | return true; 149 | } 150 | 151 | } -------------------------------------------------------------------------------- /duchain/types/gofunctiontype.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGFUNCTYPE_H 20 | #define GOLANGFUNCTYPE_H 21 | 22 | #include 23 | #include 24 | //#include 25 | #include 26 | #include 27 | 28 | using namespace KDevelop; 29 | 30 | namespace go { 31 | 32 | DECLARE_LIST_MEMBER_HASH(GoFunctionTypeData, m_returnArgs, IndexedType) 33 | 34 | class GoFunctionTypeData : public KDevelop::FunctionTypeData 35 | { 36 | public: 37 | GoFunctionTypeData() : KDevelop::FunctionTypeData() 38 | { 39 | initializeAppendedLists(m_dynamic); 40 | } 41 | 42 | GoFunctionTypeData(const GoFunctionTypeData& rhs) : KDevelop::FunctionTypeData(rhs) 43 | { 44 | //foreach( KDevelop::IndexedType type, rhs.m_returnArgs) 45 | //m_returnArgs.append(type); 46 | initializeAppendedLists(m_dynamic); 47 | copyListsFrom(rhs); 48 | } 49 | 50 | 51 | START_APPENDED_LISTS_BASE(GoFunctionTypeData, AbstractTypeData); 52 | 53 | //APPENDED_LIST_FIRST(GoFunctionTypeData, IndexedType, m_returnArgs); 54 | APPENDED_LIST(GoFunctionTypeData, IndexedType, m_returnArgs, m_arguments); 55 | 56 | END_APPENDED_LISTS(GoFunctionTypeData, m_returnArgs); 57 | }; 58 | 59 | class KDEVGODUCHAIN_EXPORT GoFunctionType : public KDevelop::FunctionType 60 | { 61 | public: 62 | typedef KDevelop::TypePtr Ptr; 63 | 64 | GoFunctionType(); 65 | 66 | GoFunctionType(GoFunctionTypeData& data); 67 | 68 | GoFunctionType(const GoFunctionType& rhs); 69 | 70 | QString toString() const override; 71 | 72 | KDevelop::AbstractType* clone() const override; 73 | 74 | uint hash() const override; 75 | 76 | void addReturnArgument(AbstractType::Ptr arg); 77 | 78 | QList returnArguments() const; 79 | 80 | bool equals(const KDevelop::AbstractType* rhs) const override; 81 | 82 | enum { 83 | Identity = 79 84 | }; 85 | 86 | enum { 87 | VariadicArgument=1<<12 88 | }; 89 | 90 | typedef GoFunctionTypeData Data; 91 | typedef KDevelop::FunctionType BaseType; 92 | 93 | protected: 94 | TYPE_DECLARE_DATA(GoFunctionType); 95 | 96 | }; 97 | 98 | } 99 | 100 | namespace KDevelop 101 | { 102 | 103 | template<> 104 | inline go::GoFunctionType* fastCast(AbstractType* from) { 105 | if ( !from || from->whichType() != AbstractType::TypeFunction ) { 106 | return 0; 107 | } else { 108 | return dynamic_cast(from); 109 | } 110 | } 111 | 112 | } 113 | 114 | #endif -------------------------------------------------------------------------------- /duchain/types/gointegraltype.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "gointegraltype.h" 20 | 21 | #include 22 | 23 | using namespace KDevelop; 24 | 25 | namespace go{ 26 | 27 | REGISTER_TYPE(GoIntegralType); 28 | 29 | GoIntegralType::GoIntegralType(const GoIntegralType& rhs) 30 | : IntegralType(copyData(*rhs.d_func())) 31 | { 32 | } 33 | 34 | GoIntegralType::GoIntegralType(GoIntegralTypeData& data) 35 | : IntegralType(data) 36 | { 37 | } 38 | 39 | GoIntegralType::GoIntegralType(uint type) 40 | : IntegralType(createData()) 41 | { 42 | setDataType(type); 43 | setModifiers(ConstModifier); 44 | } 45 | 46 | 47 | QString GoIntegralType::toString() const 48 | { 49 | 50 | TYPE_D(GoIntegralType); 51 | 52 | QString name; 53 | 54 | switch (d->m_dataType) { 55 | case TypeUint8: 56 | name="uint8"; 57 | break; 58 | case TypeUint16: 59 | name="uint16"; 60 | break; 61 | case TypeUint32: 62 | name="uint32"; 63 | break; 64 | case TypeUint64: 65 | name="uint64"; 66 | break; 67 | case TypeInt8: 68 | name="int8"; 69 | break; 70 | case TypeInt16: 71 | name="int16"; 72 | break; 73 | case TypeInt32: 74 | name="int32"; 75 | break; 76 | case TypeInt64: 77 | name="int64"; 78 | break; 79 | case TypeFloat32: 80 | name="float32"; 81 | break; 82 | case TypeFloat64: 83 | name="float64"; 84 | break; 85 | case TypeComplex64: 86 | name="complex64"; 87 | break; 88 | case TypeComplex128: 89 | name="complex128"; 90 | break; 91 | case TypeRune: 92 | name="rune"; 93 | break; 94 | case TypeUint: 95 | name="uint"; 96 | break; 97 | case TypeInt: 98 | name="int"; 99 | break; 100 | case TypeUintptr: 101 | name="uintptr"; 102 | break; 103 | case TypeString: 104 | name="string"; 105 | break; 106 | case TypeBool: 107 | name="bool"; 108 | break; 109 | case TypeByte: 110 | name="byte"; 111 | break; 112 | } 113 | 114 | return /*AbstractType::toString() + */name; 115 | } 116 | 117 | 118 | KDevelop::AbstractType* GoIntegralType::clone() const 119 | { 120 | return new GoIntegralType(*this); 121 | } 122 | 123 | uint GoIntegralType::hash() const 124 | { 125 | return 4 * KDevelop::IntegralType::hash(); 126 | } 127 | 128 | bool GoIntegralType::equals(const KDevelop::AbstractType* rhs) const 129 | { 130 | if( this == rhs ) { 131 | return true; 132 | } 133 | 134 | if ( !IntegralType::equals(rhs) ) { 135 | return false; 136 | } 137 | 138 | Q_ASSERT( fastCast(rhs) ); 139 | 140 | const GoIntegralType* type = static_cast(rhs); 141 | 142 | return d_func()->m_dataType == type->d_func()->m_dataType; 143 | } 144 | 145 | 146 | } -------------------------------------------------------------------------------- /duchain/types/gointegraltype.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGINTTYPE_H 20 | #define GOLANGINTTYPE_H 21 | 22 | #include 23 | 24 | #include "kdevgoduchain_export.h" 25 | 26 | namespace go { 27 | 28 | typedef KDevelop::IntegralTypeData GoIntegralTypeData; 29 | 30 | class KDEVGODUCHAIN_EXPORT GoIntegralType : public KDevelop::IntegralType 31 | { 32 | public: 33 | typedef KDevelop::TypePtr Ptr; 34 | 35 | /// Default constructor 36 | GoIntegralType(uint type = TypeNone); 37 | /// Copy constructor. \param rhs type to copy 38 | GoIntegralType(const GoIntegralType& rhs); 39 | /// Constructor using raw data. \param data internal data. 40 | GoIntegralType(GoIntegralTypeData& data); 41 | 42 | KDevelop::AbstractType* clone() const override; 43 | 44 | QString toString() const override; 45 | 46 | bool equals(const KDevelop::AbstractType* rhs) const override; 47 | 48 | uint hash() const override; 49 | 50 | enum GoIntegralTypes { 51 | TypeUint8=201, 52 | TypeUint16, 53 | TypeUint32, 54 | TypeUint64, 55 | TypeInt8, 56 | TypeInt16, 57 | TypeInt32, 58 | TypeInt64, 59 | TypeFloat32, 60 | TypeFloat64, 61 | TypeComplex64, 62 | TypeComplex128, 63 | TypeRune, 64 | TypeUint, 65 | TypeInt, 66 | TypeUintptr, 67 | TypeString, 68 | TypeBool, 69 | TypeByte 70 | }; 71 | 72 | enum { 73 | ///TODO: is that value OK? 74 | Identity = 78 75 | }; 76 | 77 | //GoIntegralType(uint type = TypeNone) : IntegralType(type) {} 78 | 79 | typedef KDevelop::IntegralTypeData Data; 80 | typedef KDevelop::IntegralType BaseType; 81 | 82 | protected: 83 | TYPE_DECLARE_DATA(GoIntegralType); 84 | 85 | }; 86 | 87 | } 88 | 89 | 90 | namespace KDevelop 91 | { 92 | 93 | template<> 94 | inline go::GoIntegralType* fastCast(AbstractType* from) { 95 | if ( !from || from->whichType() != AbstractType::TypeIntegral ) { 96 | return 0; 97 | } else { 98 | return dynamic_cast(from); 99 | } 100 | } 101 | 102 | } 103 | 104 | 105 | #endif -------------------------------------------------------------------------------- /duchain/types/gomaptype.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "gomaptype.h" 20 | 21 | #include 22 | 23 | using namespace KDevelop; 24 | 25 | namespace go 26 | { 27 | REGISTER_TYPE(GoMapType); 28 | 29 | GoMapType::GoMapType(const GoMapType& rhs) 30 | : KDevelop::AbstractType(copyData(*rhs.d_func())) 31 | { 32 | } 33 | 34 | GoMapType::GoMapType(GoMapTypeData& data) 35 | : KDevelop::AbstractType(data) 36 | { 37 | } 38 | 39 | GoMapType::GoMapType() 40 | : KDevelop::AbstractType(createData()) 41 | { 42 | } 43 | 44 | void GoMapType::setKeyType(AbstractType::Ptr type) 45 | { 46 | d_func_dynamic()->keyType = type->indexed(); 47 | } 48 | 49 | void GoMapType::setValueType(AbstractType::Ptr type) 50 | { 51 | d_func_dynamic()->valueType = type->indexed(); 52 | } 53 | 54 | AbstractType::Ptr GoMapType::keyType() 55 | { 56 | return d_func()->keyType.abstractType(); 57 | } 58 | 59 | AbstractType::Ptr GoMapType::valueType() 60 | { 61 | return d_func()->valueType.abstractType(); 62 | } 63 | 64 | QString GoMapType::toString() const 65 | { 66 | QString typeName("map["+d_func()->keyType.abstractType()->toString()+"]"); 67 | typeName.append(d_func()->valueType.abstractType()->toString()); 68 | return typeName; 69 | } 70 | 71 | KDevelop::AbstractType* GoMapType::clone() const 72 | { 73 | return new GoMapType(*this); 74 | } 75 | 76 | uint GoMapType::hash() const 77 | { 78 | uint hash = 6 * KDevelop::AbstractType::hash(); 79 | hash += 6 * d_func()->keyType.hash(); 80 | hash += 6 * d_func()->valueType.hash(); 81 | return hash; 82 | } 83 | 84 | void GoMapType::accept0(TypeVisitor *v) const 85 | { 86 | v->visit(this); 87 | } 88 | 89 | bool GoMapType::equals(const AbstractType* rhs) const 90 | { 91 | if(this == rhs) 92 | return true; 93 | 94 | if(!AbstractType::equals(rhs)) 95 | return false; 96 | 97 | Q_ASSERT( fastCast(rhs) ); 98 | 99 | const GoMapType* type = static_cast(rhs); 100 | 101 | if(d_func()->keyType != type->d_func()->keyType) 102 | return false; 103 | 104 | if(d_func()->valueType != type->d_func()->valueType) 105 | return false; 106 | 107 | return true; 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /duchain/types/gomaptype.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGMAPTYPE_H 20 | #define GOLANGMAPTYPE_H 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "duchain/kdevgoduchain_export.h" 27 | 28 | namespace go 29 | { 30 | 31 | class GoMapTypeData : public KDevelop::AbstractTypeData 32 | { 33 | public: 34 | GoMapTypeData() 35 | : KDevelop::AbstractTypeData() 36 | { 37 | } 38 | GoMapTypeData( const GoMapTypeData& rhs ) 39 | : KDevelop::AbstractTypeData(rhs), keyType(rhs.keyType), valueType(rhs.valueType) 40 | { 41 | } 42 | 43 | KDevelop::IndexedType keyType; 44 | KDevelop::IndexedType valueType; 45 | }; 46 | 47 | /** 48 | * I'm using custom map type insteadof MapType from KDevplatform because: 49 | * - KDevplatform's MapType tied into UnsureType and stuff which is nod needed in kdev-go 50 | * - Custom toString() implementation looks much better 51 | * - This custom type is much simpler and therefore should be a little bit more efficient 52 | * If you have a convincing reason to use KDevplatform's MapType - do it, switching types 53 | * shouldn't be hard. 54 | */ 55 | class KDEVGODUCHAIN_EXPORT GoMapType: public KDevelop::AbstractType 56 | { 57 | public: 58 | typedef KDevelop::TypePtr Ptr; 59 | 60 | /// Default constructor 61 | GoMapType(); 62 | /// Copy constructor. \param rhs type to copy 63 | GoMapType(const GoMapType& rhs); 64 | /// Constructor using raw data. \param data internal data. 65 | GoMapType(GoMapTypeData& data); 66 | 67 | void setKeyType(AbstractType::Ptr type); 68 | 69 | void setValueType(AbstractType::Ptr type); 70 | 71 | AbstractType::Ptr keyType(); 72 | 73 | AbstractType::Ptr valueType(); 74 | 75 | QString toString() const override; 76 | 77 | KDevelop::AbstractType* clone() const override; 78 | 79 | uint hash() const override; 80 | 81 | bool equals(const AbstractType* rhs) const override; 82 | 83 | void accept0(KDevelop::TypeVisitor *v) const override; 84 | 85 | enum { 86 | Identity = 105 87 | }; 88 | 89 | typedef GoMapTypeData Data; 90 | typedef KDevelop::AbstractType BaseType; 91 | 92 | protected: 93 | TYPE_DECLARE_DATA(GoMapType); 94 | }; 95 | 96 | } 97 | 98 | namespace KDevelop 99 | { 100 | 101 | template<> 102 | inline go::GoMapType* fastCast(AbstractType* from) { 103 | if ( !from || from->whichType() != AbstractType::TypeAbstract ) { 104 | return 0; 105 | } else { 106 | return dynamic_cast(from); 107 | } 108 | } 109 | 110 | } 111 | 112 | 113 | #endif -------------------------------------------------------------------------------- /duchain/types/gostructuretype.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "gostructuretype.h" 20 | 21 | #include 22 | #include 23 | 24 | using namespace KDevelop; 25 | 26 | namespace go 27 | { 28 | REGISTER_TYPE(GoStructureType); 29 | 30 | GoStructureType::GoStructureType(const GoStructureType& rhs) 31 | : KDevelop::StructureType(copyData(*rhs.d_func())) 32 | { 33 | } 34 | 35 | GoStructureType::GoStructureType(GoStructureTypeData& data) 36 | : KDevelop::StructureType(data) 37 | { 38 | } 39 | 40 | GoStructureType::GoStructureType() 41 | : KDevelop::StructureType(createData()) 42 | { 43 | } 44 | 45 | QString GoStructureType::toString() const 46 | { 47 | if(d_func()->m_prettyName.index() == 0) 48 | return "structure type"; 49 | return d_func()->m_prettyName.str(); 50 | } 51 | 52 | void GoStructureType::setContext(DUContext* context) 53 | { 54 | d_func_dynamic()->m_context = context; 55 | } 56 | 57 | DUContext* GoStructureType::context() 58 | { 59 | return d_func()->m_context.data(); 60 | } 61 | 62 | 63 | KDevelop::AbstractType* GoStructureType::clone() const 64 | { 65 | return new GoStructureType(*this); 66 | } 67 | 68 | void GoStructureType::accept0(TypeVisitor *v) const 69 | { 70 | v->visit(this); 71 | } 72 | 73 | uint GoStructureType::hash() const 74 | { 75 | uint hash = 6 * KDevelop::AbstractType::hash(); 76 | hash += 6 * d_func()->m_context.hash(); 77 | hash += 6 * d_func()->m_prettyName.hash(); 78 | return hash; 79 | } 80 | 81 | bool GoStructureType::equals(const AbstractType* rhs) const 82 | { 83 | if(this == rhs) 84 | return true; 85 | 86 | if(!AbstractType::equals(rhs)) 87 | return false; 88 | 89 | Q_ASSERT(dynamic_cast(rhs) ); 90 | 91 | const GoStructureType* type = static_cast(rhs); 92 | 93 | if(d_func()->m_context.topContextIndex() != type->d_func()->m_context.topContextIndex()) 94 | return false; 95 | 96 | if(d_func()->m_context.localIndex() != type->d_func()->m_context.localIndex()) 97 | return false; 98 | 99 | if(d_func()->m_context.context() != type->d_func()->m_context.context()) 100 | return false; 101 | 102 | //if(d_func()->m_context.data()->localScopeIdentifier() != d_func()->m_context.data()->localScopeIdentifier()) 103 | //return false; 104 | return true; 105 | } 106 | 107 | void GoStructureType::setPrettyName(QString name) 108 | { 109 | if(name.size() > 40) 110 | { 111 | name = name.left(39); 112 | name.append("..."); 113 | } 114 | d_func_dynamic()->m_prettyName = IndexedString(name); 115 | } 116 | 117 | 118 | } 119 | -------------------------------------------------------------------------------- /duchain/types/gostructuretype.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGSTRUCTTYPE_H 20 | #define GOLANGSTRUCTTYPE_H 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "kdevgoduchain_export.h" 28 | 29 | 30 | namespace go 31 | { 32 | 33 | class GoStructureTypeData : public KDevelop::StructureTypeData 34 | { 35 | public: 36 | GoStructureTypeData() 37 | : KDevelop::StructureTypeData() 38 | { 39 | m_type=0; 40 | } 41 | GoStructureTypeData( const GoStructureTypeData& rhs ) 42 | : KDevelop::StructureTypeData(rhs), m_context(rhs.m_context), m_prettyName(rhs.m_prettyName), m_type(rhs.m_type) 43 | { 44 | } 45 | 46 | KDevelop::IndexedDUContext m_context; 47 | KDevelop::IndexedString m_prettyName; 48 | uint m_type; 49 | }; 50 | 51 | 52 | class KDEVGODUCHAIN_EXPORT GoStructureType: public KDevelop::StructureType 53 | { 54 | public: 55 | typedef KDevelop::TypePtr Ptr; 56 | 57 | /// Default constructor 58 | GoStructureType(); 59 | /// Copy constructor. \param rhs type to copy 60 | GoStructureType(const GoStructureType& rhs); 61 | /// Constructor using raw data. \param data internal data. 62 | GoStructureType(GoStructureTypeData& data); 63 | 64 | void setContext(KDevelop::DUContext* context); 65 | 66 | KDevelop::DUContext* context(); 67 | 68 | void setPrettyName(QString name); 69 | 70 | void setStructureType() { d_func_dynamic()->m_type = 0; } 71 | void setInterfaceType() { d_func_dynamic()->m_type = 1; } 72 | 73 | 74 | void accept0(KDevelop::TypeVisitor *v) const override; 75 | 76 | QString toString() const override; 77 | 78 | KDevelop::AbstractType* clone() const override; 79 | 80 | uint hash() const override; 81 | 82 | bool equals(const AbstractType* rhs) const override; 83 | 84 | enum { 85 | Identity = 104 86 | }; 87 | 88 | typedef GoStructureTypeData Data; 89 | typedef KDevelop::StructureType BaseType; 90 | 91 | protected: 92 | TYPE_DECLARE_DATA(GoStructureType); 93 | }; 94 | 95 | } 96 | 97 | namespace KDevelop 98 | { 99 | 100 | template<> 101 | inline go::GoStructureType* fastCast(AbstractType* from) { 102 | if ( !from || from->whichType() != AbstractType::TypeAbstract ) { 103 | return 0; 104 | } else { 105 | return dynamic_cast(from); 106 | } 107 | } 108 | 109 | } 110 | 111 | 112 | #endif -------------------------------------------------------------------------------- /godebug.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include 20 | 21 | Q_LOGGING_CATEGORY(Go, "kdev-go") -------------------------------------------------------------------------------- /godebug.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GODEBUG_H 20 | #define GODEBUG_H 21 | 22 | #include 23 | Q_DECLARE_LOGGING_CATEGORY(Go) 24 | 25 | #endif -------------------------------------------------------------------------------- /gohighlighting.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "gohighlighting.h" 20 | 21 | Highlighting::Highlighting(QObject* parent): CodeHighlighting(parent) 22 | { 23 | 24 | } 25 | 26 | class HighlightingInstance : public KDevelop::CodeHighlightingInstance 27 | { 28 | public: 29 | HighlightingInstance(const KDevelop::CodeHighlighting* highlighting); 30 | //virtual Types typeForDeclaration(KDevelop::Declaration* decl, KDevelop::DUContext* context) const; 31 | //virtual bool useRainbowColor( KDevelop::Declaration* dec ) const; 32 | }; 33 | 34 | HighlightingInstance::HighlightingInstance(const KDevelop::CodeHighlighting* highlighting) 35 | : CodeHighlightingInstance(highlighting) 36 | { 37 | } 38 | 39 | KDevelop::CodeHighlightingInstance* Highlighting::createInstance() const 40 | { 41 | return new HighlightingInstance(this); 42 | } 43 | -------------------------------------------------------------------------------- /gohighlighting.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOLANGHIGHLIGHTING_H 20 | #define GOLANGHIGHLIGHTING_H 21 | 22 | #include 23 | 24 | class Highlighting : public KDevelop::CodeHighlighting 25 | { 26 | Q_OBJECT 27 | public: 28 | Highlighting(QObject* parent); 29 | KDevelop::CodeHighlightingInstance* createInstance() const override; 30 | 31 | }; 32 | 33 | #endif -------------------------------------------------------------------------------- /golangparsejob.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef KDEVGOLANGPARSEJOB_H 20 | #define KDEVGOLANGPARSEJOB_H 21 | 22 | #include 23 | 24 | class GoParseJob : public KDevelop::ParseJob 25 | { 26 | public: 27 | GoParseJob(const KDevelop::IndexedString& url, KDevelop::ILanguageSupport* languageSupport); 28 | 29 | protected: 30 | void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread) override; 31 | 32 | private: 33 | /** 34 | * Parses every .go file available in search paths, 35 | * looks for declarations like ` package pack //import "my_pack" ` 36 | * which means that package pack must be imported under name my_pack. 37 | * Canonical imports paths should be available before any other parsing 38 | * can begin, so it must be fast. 39 | **/ 40 | void parseCanonicalImports(); 41 | 42 | /** 43 | * extracts package's canonical import name. 44 | **/ 45 | QString extractCanonicalImport(QString string); 46 | static QHash canonicalImports; 47 | 48 | }; 49 | 50 | #endif -------------------------------------------------------------------------------- /gometalinter/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(gometalinter_SRCS plugin.cpp job.cpp problemmodel.cpp) 2 | 3 | qt5_add_resources(gometalinter_SRCS 4 | gometalinter.qrc 5 | ) 6 | kdevplatform_add_plugin(gometalinter 7 | JSON gometalinter.json 8 | SOURCES ${gometalinter_SRCS} 9 | ) 10 | 11 | target_link_libraries(gometalinter 12 | KDev::Language 13 | KDev::Project 14 | KDev::Shell 15 | KF5::ItemViews 16 | ) 17 | -------------------------------------------------------------------------------- /gometalinter/gometalinter.json: -------------------------------------------------------------------------------- 1 | { 2 | "KPlugin": { 3 | "Authors": [ 4 | { 5 | "Name": "Mikhail Ivchenko", 6 | "Name[ca@valencia]": "Mikhail Ivchenko", 7 | "Name[ca]": "Mikhail Ivchenko", 8 | "Name[cs]": "Mikhail Ivchenko", 9 | "Name[de]": "Mikhail Ivchenko", 10 | "Name[en_GB]": "Mikhail Ivchenko", 11 | "Name[es]": "Mikhail Ivchenko", 12 | "Name[fi]": "Mikhail Ivchenko", 13 | "Name[fr]": "Mikhail Ivchenko", 14 | "Name[gl]": "Mikhail Ivchenko", 15 | "Name[it]": "Mikhail Ivchenko", 16 | "Name[ko]": "Mikhail Ivchenko", 17 | "Name[nl]": "Mikhail Ivchenko", 18 | "Name[pl]": "Mikhail Ivchenko", 19 | "Name[pt]": "Mikhail Ivchenko", 20 | "Name[ru]": "Михаил Ивченко", 21 | "Name[sv]": "Mikhail Ivchenko", 22 | "Name[tr]": "Mikhail Ivchenko", 23 | "Name[uk]": "Михайло Івченко", 24 | "Name[x-test]": "xxMikhail Ivchenkoxx", 25 | "Name[zh_TW]": "Mikhail Ivchenko" 26 | } 27 | ], 28 | "Category": "Analyzers", 29 | "Description": "This plugin integrates gometalinter (static analysis tool) to KDevelop", 30 | "Description[ca@valencia]": "Aquest connector integra «gometalinter» (eina d'anàlisi estàtica) al KDevelop", 31 | "Description[ca]": "Aquest connector integra «gometalinter» (eina d'anàlisi estàtica) al KDevelop", 32 | "Description[cs]": "Tento modul integruje gometalinter (statický analytický nástroj) do KDevelop", 33 | "Description[de]": "Dieses Modul integriert gometalinter (Werkzeug zur statischen Analyse) in KDevelop", 34 | "Description[en_GB]": "This plugin integrates gometalinter (static analysis tool) to KDevelop", 35 | "Description[es]": "Este complemento integra gometalinter (herramienta de análisis sintáctico) en KDevelop", 36 | "Description[fi]": "Tämä liitännäinen yhdistää gometalinterin (staattisen analyysityökalun) KDevelopiin", 37 | "Description[fr]": "Ce module intègre gometalinter (outil d'analyse statique) dans KDevelop.", 38 | "Description[gl]": "Este complemento integra gometalinter (ferramenta de análise sintáctica) en KDevelop", 39 | "Description[it]": "Questa estensione integra gometalinter (strumento per l'analisi statica) in KDevelop", 40 | "Description[ko]": "이 플러그인은 KDevelop에 gometalinter(정적 분석 도구)를 통합합니다", 41 | "Description[nl]": "Deze plugin integreert gometalinter (statisch analyse hulpmiddel) in KDevelop", 42 | "Description[pl]": "Ta wtyczka integruje gometalinter (narzędzie analizy statycznej) w KDevelop", 43 | "Description[pt]": "Este 'plugin' integra o 'gometalinter' (ferramenta de análise estática) com o KDevelop", 44 | "Description[sv]": "Insticksprogrammet integrerar gometalinter (statiskt analysverktyg) i KDevelop", 45 | "Description[tr]": "Bu eklenti gometalinteri (değişmeyen çözümleme aracı) KDevelop'a tümleştirir.", 46 | "Description[uk]": "За допомогою цього додатка можна інтегрувати gometalinter (засіб статичного аналізу) до KDevelop", 47 | "Description[x-test]": "xxThis plugin integrates gometalinter (static analysis tool) to KDevelopxx", 48 | "Description[zh_TW]": "此外掛整合了 gometalinter (靜態分析工具) 到 KDevelop", 49 | "Id": "gometalinter", 50 | "License": "GPL", 51 | "Name": "Gometalinter Support", 52 | "Name[ca@valencia]": "Implementació del «gometalinter»", 53 | "Name[ca]": "Implementació del «gometalinter»", 54 | "Name[cs]": "Podpora Gometalinter", 55 | "Name[de]": "Unterstützung für Gometalinter", 56 | "Name[en_GB]": "Gometalinter Support", 57 | "Name[es]": "Implementación de Gometalinter", 58 | "Name[fi]": "Gometalinter-tuki", 59 | "Name[fr]": "Prise en charge de Gometalinter", 60 | "Name[gl]": "Compatibilidade con Gometalinter", 61 | "Name[it]": "Supporto per gometalinter", 62 | "Name[ko]": "Gometalinter 지원", 63 | "Name[nl]": "Ondersteuning voor Gometalinter", 64 | "Name[pl]": "Obsługa Gometalinter", 65 | "Name[pt]": "Suporte para o Gometalinter", 66 | "Name[sv]": "Gometalinter-stöd", 67 | "Name[tr]": "Gometalinter Desteği", 68 | "Name[uk]": "Підтримка Gometalinter", 69 | "Name[x-test]": "xxGometalinter Supportxx", 70 | "Name[zh_TW]": "Gometalinter 支援", 71 | "ServiceTypes": [ 72 | "KDevelop/Plugin" 73 | ] 74 | }, 75 | "X-KDevelop-Category": "Global", 76 | "X-KDevelop-IRequired": [ 77 | "org.kdevelop.IExecutePlugin" 78 | ], 79 | "X-KDevelop-Mode": "GUI" 80 | } 81 | -------------------------------------------------------------------------------- /gometalinter/gometalinter.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | gometalinter.rc 5 | 6 | 7 | -------------------------------------------------------------------------------- /gometalinter/gometalinter.rc: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /gometalinter/job.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop gometalinter support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "job.h" 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace GoMetaLinter 23 | { 24 | 25 | 26 | Job::Job(const QUrl &workingDirectory, const QString &path, QObject *parent) 27 | : KDevelop::OutputExecuteJob(parent) 28 | , m_timer(new QElapsedTimer) 29 | { 30 | setJobName(i18n("Go Meta Linter Analysis")); 31 | 32 | setCapabilities(KJob::Killable); 33 | setStandardToolView(KDevelop::IOutputView::TestView); 34 | setBehaviours(KDevelop::IOutputView::AutoScroll); 35 | 36 | setProperties(KDevelop::OutputExecuteJob::JobProperty::DisplayStdout); 37 | setProperties(KDevelop::OutputExecuteJob::JobProperty::DisplayStderr); 38 | setProperties(KDevelop::OutputExecuteJob::JobProperty::PostProcessOutput); 39 | 40 | setWorkingDirectory(workingDirectory); 41 | QStringList commandLine = {"gometalinter", "--aggregate", "--sort=path", path}; 42 | *this << commandLine; 43 | m_projectRootPath = KDevelop::Path(workingDirectory); 44 | } 45 | 46 | Job::~Job() 47 | { 48 | doKill(); 49 | } 50 | 51 | void Job::postProcessStdout(const QStringList& lines) 52 | { 53 | static const auto problemRegex = QRegularExpression(QStringLiteral("^([^:]*):([^:]*):([^:]*):([^:]*): ([^:]*)$")); 54 | 55 | QRegularExpressionMatch match; 56 | 57 | QVector problems; 58 | 59 | foreach (const QString & line, lines) { 60 | match = problemRegex.match(line); 61 | if (match.hasMatch()) { 62 | KDevelop::IProblem::Ptr problem(new KDevelop::DetectedProblem(i18n("Go Meta Linter"))); 63 | if(match.captured(4) == QStringLiteral("warning")) 64 | { 65 | problem->setSeverity(KDevelop::IProblem::Warning); 66 | } 67 | if(match.captured(4) == QStringLiteral("error")) 68 | { 69 | problem->setSeverity(KDevelop::IProblem::Error); 70 | } 71 | problem->setDescription(match.captured(5)); 72 | KDevelop::DocumentRange range; 73 | range.document = KDevelop::IndexedString(KDevelop::Path(m_projectRootPath, match.captured(1)).toLocalFile()); 74 | range.setBothLines(match.captured(2).toInt() - 1); 75 | if(!match.captured(3).isEmpty()) 76 | { 77 | range.setBothColumns(match.captured(3).toInt() - 1); 78 | } 79 | problem->setFinalLocation(range); 80 | problems.append(problem); 81 | } 82 | } 83 | 84 | emit problemsDetected(problems); 85 | 86 | if (status() == KDevelop::OutputExecuteJob::JobStatus::JobRunning) { 87 | KDevelop::OutputExecuteJob::postProcessStdout(lines); 88 | } 89 | } 90 | 91 | void Job::start() 92 | { 93 | m_timer->restart(); 94 | KDevelop::OutputExecuteJob::start(); 95 | } 96 | 97 | void Job::childProcessError(QProcess::ProcessError e) 98 | { 99 | QString message; 100 | 101 | switch (e) { 102 | case QProcess::FailedToStart: 103 | message = i18n("Failed to start Go Meta Linter from \"%1\".", commandLine()[0]); 104 | break; 105 | 106 | case QProcess::Crashed: 107 | if (status() != KDevelop::OutputExecuteJob::JobStatus::JobCanceled) { 108 | message = i18n("Go Meta Linter crashed."); 109 | } 110 | break; 111 | 112 | case QProcess::Timedout: 113 | message = i18n("Go Meta Linter process timed out."); 114 | break; 115 | 116 | case QProcess::WriteError: 117 | message = i18n("Write to Go Meta Linter process failed."); 118 | break; 119 | 120 | case QProcess::ReadError: 121 | message = i18n("Read from Go Meta Linter process failed."); 122 | break; 123 | 124 | case QProcess::UnknownError: 125 | break; 126 | } 127 | 128 | if (!message.isEmpty()) { 129 | KMessageBox::error(qApp->activeWindow(), message, i18n("Go Meta Linter Error")); 130 | } 131 | 132 | KDevelop::OutputExecuteJob::childProcessError(e); 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /gometalinter/job.h: -------------------------------------------------------------------------------- 1 | /* KDevelop gometalinter support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef GOMETALINTER_JOB_H 12 | #define GOMETALINTER_JOB_H 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | class QElapsedTimer; 19 | 20 | namespace GoMetaLinter 21 | { 22 | 23 | class Job : public KDevelop::OutputExecuteJob 24 | { 25 | Q_OBJECT 26 | 27 | public: 28 | explicit Job(const QUrl &workingDirectory, const QString &path, QObject *parent = nullptr); 29 | ~Job() override; 30 | 31 | void start() override; 32 | 33 | Q_SIGNALS: 34 | void problemsDetected(const QVector& problems); 35 | 36 | protected slots: 37 | void postProcessStdout(const QStringList& lines) override; 38 | 39 | void childProcessError(QProcess::ProcessError processError) override; 40 | 41 | protected: 42 | QScopedPointer m_timer; 43 | 44 | KDevelop::Path m_projectRootPath; 45 | }; 46 | 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /gometalinter/plugin.h: -------------------------------------------------------------------------------- 1 | /* KDevelop gometalinter support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef GOMETALINTER_PLUGIN_H 12 | #define GOMETALINTER_PLUGIN_H 13 | 14 | #include "problemmodel.h" 15 | #include "job.h" 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | namespace GoMetaLinter 22 | { 23 | 24 | class Plugin : public KDevelop::IPlugin 25 | { 26 | Q_OBJECT 27 | public: 28 | explicit Plugin(QObject *parent, const QVariantList&); 29 | ~Plugin() override; 30 | KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context* context, QWidget* parent) override; 31 | void run(KDevelop::IProject* project, const QString &path); 32 | bool isRunning(); 33 | private: 34 | void updateActions(); 35 | void projectClosed(KDevelop::IProject* project); 36 | void killJob(); 37 | void result(KJob* job); 38 | void run(bool checkProject); 39 | 40 | KDevelop::IProject* m_currentProject; 41 | 42 | QAction* m_menuActionFile; 43 | QAction* m_menuActionProject; 44 | QAction* m_contextActionFile; 45 | QAction* m_contextActionProject; 46 | QAction* m_contextActionProjectItem; 47 | 48 | ProblemModel* m_model; 49 | Job* m_job; 50 | }; 51 | 52 | } 53 | 54 | 55 | #endif //KDEVGOPLUGIN_PLUGIN_H 56 | -------------------------------------------------------------------------------- /gometalinter/problemmodel.cpp: -------------------------------------------------------------------------------- 1 | /* KDevelop gometalinter support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #include "problemmodel.h" 12 | 13 | #include "plugin.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | namespace GoMetaLinter 23 | { 24 | 25 | inline KDevelop::ProblemModelSet* problemModelSet() 26 | { 27 | return KDevelop::ICore::self()->languageController()->problemModelSet(); 28 | } 29 | 30 | static const QString problemModelId = QStringLiteral("Go Meta Linter"); 31 | 32 | ProblemModel::ProblemModel(Plugin* plugin) 33 | : KDevelop::ProblemModel(plugin), 34 | m_plugin(plugin), 35 | m_project(nullptr) 36 | { 37 | setFeatures(CanDoFullUpdate | ScopeFilter | SeverityFilter | Grouping | CanByPassScopeFilter); 38 | reset(); 39 | problemModelSet()->addModel(problemModelId, i18n("Go Meta Linter"), this); 40 | } 41 | 42 | ProblemModel::~ProblemModel() 43 | { 44 | problemModelSet()->removeModel(problemModelId); 45 | } 46 | 47 | KDevelop::IProject* ProblemModel::project() const 48 | { 49 | return m_project; 50 | } 51 | 52 | bool ProblemModel::problemExists(KDevelop::IProblem::Ptr newProblem) 53 | { 54 | for (auto problem : m_problems) { 55 | if (newProblem->source() == problem->source() && 56 | newProblem->severity() == problem->severity() && 57 | newProblem->finalLocation() == problem->finalLocation() && 58 | newProblem->description() == problem->description() && 59 | newProblem->explanation() == problem->explanation()) 60 | return true; 61 | } 62 | 63 | return false; 64 | } 65 | 66 | void ProblemModel::addProblems(const QVector& problems) 67 | { 68 | static int maxLength = 0; 69 | 70 | if (m_problems.isEmpty()) { 71 | maxLength = 0; 72 | } 73 | 74 | for (auto problem : problems) { 75 | 76 | if (problemExists(problem)) { 77 | continue; 78 | } 79 | 80 | m_problems.append(problem); 81 | addProblem(problem); 82 | 83 | // This performs adjusting of columns width in the ProblemsView 84 | if (maxLength < problem->description().length()) { 85 | maxLength = problem->description().length(); 86 | setProblems(m_problems); 87 | } 88 | } 89 | } 90 | 91 | void ProblemModel::reset() 92 | { 93 | reset(nullptr, QString()); 94 | } 95 | 96 | void ProblemModel::reset(KDevelop::IProject* project, const QString& path) 97 | { 98 | m_project = project; 99 | m_path = path; 100 | 101 | clearProblems(); 102 | m_problems.clear(); 103 | 104 | QString tooltip = i18nc("@info:tooltip", "Re-Run Last Go Meta Linter Analysis"); 105 | setFullUpdateTooltip(tooltip); 106 | } 107 | 108 | void ProblemModel::show() 109 | { 110 | problemModelSet()->showModel(problemModelId); 111 | } 112 | 113 | void ProblemModel::forceFullUpdate() 114 | { 115 | if (m_project && !m_plugin->isRunning()) { 116 | m_plugin->run(m_project, m_path); 117 | } 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /gometalinter/problemmodel.h: -------------------------------------------------------------------------------- 1 | /* KDevelop gometalinter support 2 | * 3 | * Copyright 2017 Mikhail Ivchenko 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; either version 2 8 | * of the License, or (at your option) any later version. 9 | */ 10 | 11 | #ifndef GOMETALINTER_PROBLEMMODEL_H 12 | #define GOMETALINTER_PROBLEMMODEL_H 13 | 14 | #include 15 | 16 | namespace KDevelop 17 | { 18 | class IProject; 19 | } 20 | 21 | namespace GoMetaLinter 22 | { 23 | 24 | class Plugin; 25 | 26 | class ProblemModel : public KDevelop::ProblemModel 27 | { 28 | public: 29 | explicit ProblemModel(Plugin* plugin); 30 | ~ProblemModel() override; 31 | 32 | KDevelop::IProject* project() const; 33 | 34 | void addProblems(const QVector& problems); 35 | 36 | void reset(); 37 | void reset(KDevelop::IProject* project, const QString& path); 38 | 39 | void show(); 40 | 41 | void forceFullUpdate() override; 42 | 43 | private: 44 | bool problemExists(KDevelop::IProblem::Ptr newProblem); 45 | 46 | using KDevelop::ProblemModel::setProblems; 47 | 48 | Plugin* m_plugin; 49 | 50 | KDevelop::IProject* m_project; 51 | QString m_path; 52 | 53 | QVector m_problems; 54 | }; 55 | 56 | } 57 | 58 | #endif -------------------------------------------------------------------------------- /kdevgo.json: -------------------------------------------------------------------------------- 1 | { 2 | "KPlugin": { 3 | "Category": "Language Support", 4 | "Icon": "text-x-go", 5 | "Id": "kdevgo", 6 | "License": "GPL", 7 | "Name": "Go Support", 8 | "Name[bs]": "Go podrška", 9 | "Name[ca@valencia]": "Implementació del Go", 10 | "Name[ca]": "Implementació del Go", 11 | "Name[cs]": "Podpora Go", 12 | "Name[de]": "Unterstützung für Go", 13 | "Name[en_GB]": "Go Support", 14 | "Name[es]": "Implementación de Go", 15 | "Name[fi]": "Go-tuki", 16 | "Name[fr]": "Assistance Go", 17 | "Name[gl]": "Compatibilidade con Go", 18 | "Name[hu]": "Go támogatás", 19 | "Name[it]": "Supporto per Go", 20 | "Name[ko]": "Go 지원", 21 | "Name[nl]": "Ondersteuning voor Go", 22 | "Name[pl]": "Obsługa Go", 23 | "Name[pt]": "Suporte para Go", 24 | "Name[pt_BR]": "Suporte a Go", 25 | "Name[ru]": "Поддержка Go", 26 | "Name[sk]": "Podpora Go", 27 | "Name[sv]": "Go-support", 28 | "Name[tr]": "Go Desteği", 29 | "Name[uk]": "Підтримка Go", 30 | "Name[x-test]": "xxGo Supportxx", 31 | "Name[zh_CN]": "Go 支持", 32 | "Name[zh_TW]": "Go 支援", 33 | "ServiceTypes": [ 34 | "KDevelop/Plugin" 35 | ] 36 | }, 37 | "X-KDevelop-Interfaces": [ 38 | "ILanguageSupport" 39 | ], 40 | "X-KDevelop-Language": "Go", 41 | "X-KDevelop-Mode": "NoGUI", 42 | "X-KDevelop-SupportedMimeTypes": [ 43 | "text/x-go" 44 | ] 45 | } 46 | -------------------------------------------------------------------------------- /kdevgoplugin.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "kdevgoplugin.h" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "codecompletion/model.h" 29 | #include "golangparsejob.h" 30 | 31 | K_PLUGIN_FACTORY_WITH_JSON(GoPluginFactory, "kdevgo.json", registerPlugin(); ) 32 | 33 | using namespace KDevelop; 34 | 35 | 36 | GoPlugin::GoPlugin(QObject* parent, const QVariantList&) 37 | : KDevelop::IPlugin("kdevgoplugin", parent), 38 | ILanguageSupport() 39 | { 40 | qCDebug(Go) << "Go Language Plugin is loaded\n"; 41 | 42 | CodeCompletionModel* codeCompletion = new go::CodeCompletionModel(this); 43 | new KDevelop::CodeCompletion(this, codeCompletion, name()); 44 | 45 | m_highlighting = new Highlighting(this); 46 | } 47 | 48 | GoPlugin::~GoPlugin() 49 | { 50 | } 51 | 52 | ParseJob* GoPlugin::createParseJob(const IndexedString& url) 53 | { 54 | qCDebug(Go) << "Creating golang parse job\n"; 55 | return new GoParseJob(url, this); 56 | } 57 | 58 | QString GoPlugin::name() const 59 | { 60 | return "Golang"; 61 | } 62 | 63 | KDevelop::ICodeHighlighting* GoPlugin::codeHighlighting() const 64 | { 65 | return m_highlighting; 66 | } 67 | 68 | KDevelop::SourceFormatterItemList GoPlugin::sourceFormatterItems() const 69 | { 70 | SourceFormatterItemList result; 71 | SourceFormatterStyleItem item; 72 | item.engine = "customscript"; 73 | item.style = SourceFormatterStyle("Go fmt"); 74 | item.style.setMimeTypes({SourceFormatterStyle::MimeHighlightPair{"text/x-go", "Go"}}); 75 | item.style.setCaption("Go fmt support"); 76 | item.style.setContent("gofmt $FILE"); 77 | result.append(item); 78 | return result; 79 | } 80 | 81 | #include "kdevgoplugin.moc" 82 | -------------------------------------------------------------------------------- /kdevgoplugin.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | /** 20 | * This plugin is heavily influenced by kdev-php and kdev-qmljs plugins. 21 | * If you have problems figuring out how something works, try looking for 22 | * similar code in these plugins, it should be better documented there. 23 | */ 24 | 25 | #ifndef KDEVGOPLUGIN_H 26 | #define KDEVGOPLUGIN_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include "gohighlighting.h" 33 | 34 | namespace KDevelop 35 | { 36 | class IProject; 37 | class IDocument; 38 | class ParseJob; 39 | } 40 | 41 | class GoPlugin : public KDevelop::IPlugin, public KDevelop::ILanguageSupport 42 | { 43 | Q_OBJECT 44 | Q_INTERFACES( KDevelop::ILanguageSupport ) 45 | public: 46 | explicit GoPlugin(QObject* parent, const QVariantList &args); 47 | 48 | ~GoPlugin() override; 49 | 50 | KDevelop::ParseJob* createParseJob(const KDevelop::IndexedString& url) override; 51 | QString name() const override; 52 | KDevelop::SourceFormatterItemList sourceFormatterItems() const override; 53 | 54 | KDevelop::ICodeHighlighting* codeHighlighting() const override; 55 | 56 | private: 57 | Highlighting* m_highlighting; 58 | }; 59 | #endif -------------------------------------------------------------------------------- /parser/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | kdevpgqt_generate(go_parser_SRC go NAMESPACE go DEBUG_VISITOR TOKEN_TEXT GENERATE_LEXER 2 | "${CMAKE_CURRENT_SOURCE_DIR}/go.g") 3 | 4 | set(go_parser_lib_SRC 5 | parsesession.cpp 6 | ) 7 | 8 | add_library(kdevgoparser SHARED ${go_parser_SRC} ${go_parser_lib_SRC}) 9 | include(GenerateExportHeader) 10 | generate_export_header(kdevgoparser BASE_NAME kdevgoparser EXPORT_MACRO_NAME KDEVGOPARSER_EXPORT) 11 | target_link_libraries( kdevgoparser LINK_PRIVATE 12 | KDev::Language 13 | ) 14 | target_include_directories(kdevgoparser SYSTEM PUBLIC ${KDEVPGQT_INCLUDE_DIR}) 15 | 16 | add_executable(go_parser main.cpp) 17 | target_link_libraries(go_parser 18 | kdevgoparser 19 | KDev::Language 20 | ) 21 | install(TARGETS kdevgoparser DESTINATION ${INSTALL_TARGETS_DEFAULT_ARGS}) 22 | 23 | if(BUILD_TESTING) 24 | add_subdirectory(test) 25 | endif() 26 | -------------------------------------------------------------------------------- /parser/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #include "golexer.h" 20 | #include "godebugvisitor.h" 21 | 22 | #include "parsesession.h" 23 | 24 | int main(int argc, char** argv) 25 | { 26 | if(argc < 2) 27 | return 2; 28 | qDebug() << argv[1]; 29 | QFile file(argv[1]); 30 | if(! file.open(QIODevice::ReadOnly | QIODevice::Text)) 31 | { 32 | return 1; 33 | } 34 | QTextStream in(&file); 35 | in.setCodec("UTF-8"); 36 | QByteArray code = in.readAll().toUtf8(); 37 | ParseSession session(code, 1); 38 | bool result=session.startParsing(); 39 | 40 | go::DebugVisitor visitor(getLexer(session), code); 41 | visitor.visitNode(session.ast()); 42 | return !result ? 3 : 0; 43 | } 44 | -------------------------------------------------------------------------------- /parser/parsesession.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef KDEVGOLANGPARSESESSION_H 20 | #define KDEVGOLANGPARSESESSION_H 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "kdevgoparser_export.h" 29 | #include "parser/goast.h" 30 | 31 | namespace KDevPG 32 | { 33 | class MemoryPool; 34 | } 35 | 36 | namespace go 37 | { 38 | class Lexer; 39 | class Parser; 40 | class StartAst; 41 | } 42 | 43 | typedef QPair SimpleUse; 44 | 45 | class KDEVGOPARSER_EXPORT ParseSession 46 | { 47 | public: 48 | 49 | ParseSession(const QByteArray& contents, int priority, bool appendWithNewline=true); 50 | 51 | virtual ~ParseSession(); 52 | 53 | static KDevelop::IndexedString languageString(); 54 | 55 | bool startParsing(); 56 | 57 | bool parseExpression(go::ExpressionAst **node); 58 | 59 | go::StartAst* ast(); 60 | 61 | QString symbol(qint64 index); 62 | 63 | KDevelop::RangeInRevision findRange(go::AstNode* from, go::AstNode* to); 64 | 65 | KDevelop::IndexedString currentDocument(); 66 | 67 | void setCurrentDocument(const KDevelop::IndexedString& document); 68 | 69 | KDevelop::IndexedString url(); 70 | 71 | QList contextForImport(QString package); 72 | 73 | QList contextForThisPackage(KDevelop::IndexedString package); 74 | 75 | QList problems() const; 76 | 77 | /** 78 | * Schedules for parsing with given priority and features. 79 | * NOTE this will not schedule recursive imports (imports of imports and so on) 80 | * to schedule any file use BackgroundParser instead 81 | **/ 82 | bool scheduleForParsing(const KDevelop::IndexedString& url, int priority, KDevelop::TopDUContext::Features features); 83 | 84 | /** 85 | * Reschedules this file with same features. 86 | **/ 87 | void rescheduleThisFile(); 88 | 89 | void reparseImporters(KDevelop::DUContext* context); 90 | 91 | void setFeatures(KDevelop::TopDUContext::Features features); 92 | 93 | QString textForNode(go::AstNode* node); 94 | 95 | void setIncludePaths(const QList &paths); 96 | 97 | void setCanonicalImports(QHash* imports); 98 | 99 | 100 | /** 101 | * Returns doc comment preceding given token. 102 | * GoDoc comments are multilined dash-star-style comments (/\*) 103 | * or several consecutive single-lined dash-dash-style comments (//) 104 | * with no empty line between them. 105 | * Comment must start on a new line and end a line before given declaration. 106 | **/ 107 | QByteArray commentBeforeToken(qint64 token); 108 | 109 | /** 110 | * Don't use this function! 111 | * Most of the times you don't need to access lexer of parseSession directly, 112 | * This only exists, because parser test application uses DebugVisitor, which needs a lexer 113 | */ 114 | friend go::Lexer* getLexer(const ParseSession& session) { return session.m_lexer; } 115 | 116 | void mapAstUse(go::AstNode* node, const SimpleUse& use) 117 | { 118 | Q_UNUSED(node); 119 | Q_UNUSED(use); 120 | } 121 | 122 | private: 123 | 124 | bool lex(); 125 | 126 | KDevPG::MemoryPool* m_pool; 127 | go::Lexer* m_lexer; 128 | go::Parser* m_parser; 129 | go::StartAst* m_ast; 130 | QByteArray m_contents; 131 | 132 | int m_priority; 133 | KDevelop::IndexedString m_document; 134 | KDevelop::TopDUContext::Features m_features; 135 | bool forExport; 136 | QList m_includePaths; 137 | QHash* m_canonicalImports; 138 | 139 | QList m_problems; 140 | }; 141 | 142 | #endif 143 | -------------------------------------------------------------------------------- /parser/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ecm_add_test(parsertest.cpp 2 | LINK_LIBRARIES 3 | kdevgoparser 4 | Qt5::Test 5 | Qt5::Core 6 | KDev::Language 7 | KDev::Tests 8 | ) -------------------------------------------------------------------------------- /parser/test/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | //import "fmt" 4 | 5 | 6 | type functest func() int 7 | 8 | 9 | func main() { 10 | 11 | var testvar functest 12 | var fvar mystruct2 13 | var fvar2 mystruct 14 | auto2 := functest(); 15 | 16 | functest(functest(fvar)) 17 | 18 | fvar = functest(fvar) 19 | fvar.abc = functest(fvar.str.var1, functest(fvar2.var1)); 20 | 21 | pop, nop := fvar.mymethod(fvar2); 22 | ftg := fvar.abc 23 | fvar.mymethod(); 24 | 25 | 26 | fmt.Println(fvar.abc); 27 | 28 | var nmake map[int]func(); 29 | 30 | mmake := make(map[rune]mystruct, fvar) 31 | 32 | var sttest interface { Area() int } 33 | } 34 | 35 | type Newtype int; 36 | 37 | func (i mystruct2) mymethod(s string) (myint int, mybool bool) { 38 | i.abc; 39 | s = myint + mybool; 40 | } 41 | 42 | type mystruct2 struct { abc rune; str mystruct; } 43 | 44 | type mystruct struct { var1 int } 45 | -------------------------------------------------------------------------------- /parser/test/parsertest.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | * Copyright (C) 2014 by Pavel Petrushkov * 3 | * * 4 | * This program is free software; you can redistribute it and/or * 5 | * modify it under the terms of the GNU General Public License * 6 | * as published by the Free Software Foundation; either version 2 * 7 | * of the License, or (at your option) any later version. * 8 | * * 9 | * This program is distributed in the hope that it will be useful, * 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 | * GNU General Public License for more details. * 13 | * * 14 | * You should have received a copy of the GNU General Public License * 15 | * along with this program; if not, write to the Free Software * 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 17 | *************************************************************************************/ 18 | 19 | #ifndef GOPARSERTEST 20 | #define GOPARSERTEST 21 | 22 | #include 23 | #include 24 | 25 | namespace go { 26 | 27 | class Lexer; 28 | class AstNode; 29 | 30 | class ParserTest : public QObject 31 | { 32 | Q_OBJECT 33 | public: 34 | ParserTest(); 35 | 36 | private slots: 37 | void initTestCase(); 38 | void testKeyWords(); 39 | void testOperators(); 40 | void testRunes(); 41 | void testNumbers(); 42 | void testNumbers_data(); 43 | void testCommentsAreIgnored(); 44 | void testCommentsAreIgnored_data(); 45 | void testMultiLineStrings(); 46 | void testMultiLineStrings_data(); 47 | void testBasicTypes(); 48 | void testIfClause(); 49 | void testFuncTypes(); 50 | void testForRangeLoop(); 51 | void testForSingleConditionalLoop(); 52 | void testForWithForClauseLoop(); 53 | void testForWithEmptySingleConditionLoop(); 54 | void testShortVarDeclaration(); 55 | void testShortVarDeclaration_data(); 56 | void testEmptyLabeledStmt(); 57 | void testMapKeyLiteralValue(); //Go 1.5 feature 58 | private: 59 | QByteArray getCodeFromNode(const QByteArray &code, go::Lexer *lexer, go::AstNode *node); 60 | }; 61 | 62 | } 63 | 64 | #endif -------------------------------------------------------------------------------- /parser/test/test.file: -------------------------------------------------------------------------------- 1 | break default func interface select 2 | case defer go map struct 3 | chan else goto package switch 4 | const fallthrough if range type 5 | continue for import return var 6 | + & += &= && == != ( ) 7 | - | -= |= || < <= [ ] 8 | * ^ *= ^= <- > >= { } 9 | / << /= <<= ++ = := , ; 10 | % >> %= >>= -- ! ... . : 11 | &^ &^= 12 | 13 | 14 | bui /* hi*/ 15 | an 16 | /** 17 | * oh my god */ 18 | //oneliner another word 19 | 20 | 21 | arkadi arkadi 22 | wtf 23 | аркадий лалка 24 | 25 | break 26 | _thisidstartswith_ 27 | 42 28 | 0600 29 | 0xBadFace 30 | 170141183460469231731687303715884105727 31 | 32 | 0. 33 | 72.40 34 | 072.40 // == 72.40 35 | 2.71828 36 | 1.e+0 37 | 6.67428e-11 38 | 1E6 39 | .25 40 | .12345E+5 41 | 42 | 0i 43 | 011i // == 11i 44 | 0.i 45 | 2.71828i 46 | 1.e+0i 47 | 6.67428e-11i 48 | 1E6i 49 | .25i 50 | .12345E+5i 51 | 52 | 'a' 53 | 'ä' 54 | '本' 55 | '\t' 56 | '\u12e4' 57 | '\U00101234' 58 | '\x07' 59 | '\xff' 60 | '\000' 61 | '\007' 62 | '\377' 63 | 64 | "Hello` World!\n" 65 | defer 66 | "su\u0010p?" 67 | `арка"дий` 68 | defer 69 | `jkj\000kj` 70 | -------------------------------------------------------------------------------- /parser/test/utf8.go: -------------------------------------------------------------------------------- 1 | hello ŝ mygod 2 | аркадий лалка 3 | "\\" "a" "\"" "b" "\n" "c" 4 | 5 | 'y's 6 | 'щ'h 7 | '日' adaksjdlas 8 | 'k' --------------------------------------------------------------------------------