Syntax Highlighting för C++ Beter Sig Konstigt i VS Code på Ubuntu 22.04

Permalänk
Medlem

Syntax Highlighting för C++ Beter Sig Konstigt i VS Code på Ubuntu 22.04

Hej,
Jag lär mig C++ på Linux och fick nyss ett skumt problem med syntax highlighting/semantic highlighting i VS Code, bild nedan:

Ner till rad 56 är "std::cout" inte ljusblått som från rad 57. Hur gör jag så att "cout"-delen blir ljusblå som på rad 57?

Det må vara en kosmetisk detalj men jag får inte samma fel i andra filer med många "std::cout". Om jag klipper och klistrar koden från den ursprungliga filen till en ny fil så kvarstår problemet.

Jag har kollat på inställningar för syntax highlighting och semantic highlighting, ingen av dessa har hjälpt något. Koden kompilerar felfritt med g++ och exekverar utan problem.
Om det behövs kan jag lägga upp koden i sin helhet vid förfrågan.

Permalänk

Det är svårt att se när du inte skickar hela koden. I alla fall, när du skriver '\n' är det en char (till skillnad från char[] eller kanske string i C++), så det kan ställa till det med highlightingen.

Permalänk
Medlem
Skrivet av xGreenRed:

Det är svårt att se när du inte skickar hela koden. I alla fall, när du skriver '\n' är det en char (till skillnad från char[] eller kanske string i C++), så det kan ställa till det med highlightingen.

All kod:

#include <limits> #include <cstddef> #include <string> #include <bit> #include <type_traits> #include <sstream> #include <iostream> template <typename T> requires std::is_signed_v <T> std::string to_power_of_two(T val) { const bool negative = val < 0; using U = std::make_unsigned_t <T>; const U uval = negative ? static_cast <U> (- val) : static_cast <U> (val); const auto width = std::bit_width(uval) - negative; std::ostringstream ss; ss << (negative ? '-' : '+') << "2^" << width; if (! negative) { ss << "-1"; } return ss.str (); } template <typename T> requires std::is_unsigned_v <T> std::string to_power_of_two(T val) { std::ostringstream ss; ss << "+2^" << std::bit_width(val); return ss.str (); } int main() { std::cout << "Below is a list of max and min values for all numeric types on this system: " << '\n'; std::cout << "*********** Integer Ranges ************" << '\n'; std::cout << "**** short int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<short>::min() /* << " = " << to_power_of_two(std::numeric_limits<short>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<short>::max() << " = " << to_power_of_two(std::numeric_limits<short>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned short int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned short int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned short int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned short int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned short int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<int>::min() << " = " << to_power_of_two(std::numeric_limits<int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<int>::max() << " = " << to_power_of_two(std::numeric_limits<int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<long int>::min() << " = " << to_power_of_two(std::numeric_limits<long int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<long int>::max() << " = " << to_power_of_two(std::numeric_limits<long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned long int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned long int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned long int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** long long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<long long int>::min() << " = " << to_power_of_two(std::numeric_limits<long long int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<long long int>::max() << " = " << to_power_of_two(std::numeric_limits<long long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned long long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned long long int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned long long int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned long long int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned long long int>::max()) << '\n'; std::cout << '\n'; std::cout << "*********** Floating Point Ranges ************" << '\n'; std::cout << "**** float ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<float>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<float>::max() << '\n'; std::cout << '\n'; std::cout << "**** double ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<double>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<double>::max() << '\n'; std::cout << '\n'; std::cout << "**** long double ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<long double>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<long double>::max() << '\n'; return 0; }

Förtydligande av kod.
Permalänk
Medlem

Finns en utmärkt tag: [code] kod här tack [/code].

#include <limits> #include <cstddef> #include <string> #include <bit> #include <type_traits> #include <sstream> #include <iostream> template <typename T> requires std::is_signed_v <T> std::string to_power_of_two(T val) { const bool negative = val < 0; using U = std::make_unsigned_t <T>; const U uval = negative ? static_cast <U> (- val) : static_cast <U> (val); const auto width = std::bit_width(uval) - negative; std::ostringstream ss; ss << (negative ? '-' : '+') << "2^" << width; if (! negative) { ss << "-1"; } return ss.str (); } template <typename T> requires std::is_unsigned_v <T> std::string to_power_of_two(T val) { std::ostringstream ss; ss << "+2^" << std::bit_width(val); return ss.str (); } int main() { std::cout << "Below is a list of max and min values for all numeric types on this system: " << '\n'; std::cout << "*********** Integer Ranges ************" << '\n'; std::cout << "**** short int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<short>::min() /* << " = " << to_power_of_two(std::numeric_limits<short>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<short>::max() << " = " << to_power_of_two(std::numeric_limits<short>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned short int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned short int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned short int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned short int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned short int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<int>::min() << " = " << to_power_of_two(std::numeric_limits<int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<int>::max() << " = " << to_power_of_two(std::numeric_limits<int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<long int>::min() << " = " << to_power_of_two(std::numeric_limits<long int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<long int>::max() << " = " << to_power_of_two(std::numeric_limits<long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned long int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned long int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned long int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** long long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<long long int>::min() << " = " << to_power_of_two(std::numeric_limits<long long int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<long long int>::max() << " = " << to_power_of_two(std::numeric_limits<long long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned long long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned long long int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned long long int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned long long int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned long long int>::max()) << '\n'; std::cout << '\n'; std::cout << "*********** Floating Point Ranges ************" << '\n'; std::cout << "**** float ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<float>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<float>::max() << '\n'; std::cout << '\n'; std::cout << "**** double ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<double>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<double>::max() << '\n'; std::cout << '\n'; std::cout << "**** long double ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<long double>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<long double>::max() << '\n'; return 0; }

Visa signatur

:(){ :|:& };:

🏊🏻‍♂️   🚴🏻‍♂️   🏃🏻‍♂️   ☕

Permalänk
Medlem
Skrivet av GLaDER:

Finns en utmärkt tag: [code] kod här tack [/code].

#include <limits> #include <cstddef> #include <string> #include <bit> #include <type_traits> #include <sstream> #include <iostream> template <typename T> requires std::is_signed_v <T> std::string to_power_of_two(T val) { const bool negative = val < 0; using U = std::make_unsigned_t <T>; const U uval = negative ? static_cast <U> (- val) : static_cast <U> (val); const auto width = std::bit_width(uval) - negative; std::ostringstream ss; ss << (negative ? '-' : '+') << "2^" << width; if (! negative) { ss << "-1"; } return ss.str (); } template <typename T> requires std::is_unsigned_v <T> std::string to_power_of_two(T val) { std::ostringstream ss; ss << "+2^" << std::bit_width(val); return ss.str (); } int main() { std::cout << "Below is a list of max and min values for all numeric types on this system: " << '\n'; std::cout << "*********** Integer Ranges ************" << '\n'; std::cout << "**** short int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<short>::min() /* << " = " << to_power_of_two(std::numeric_limits<short>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<short>::max() << " = " << to_power_of_two(std::numeric_limits<short>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned short int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned short int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned short int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned short int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned short int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<int>::min() << " = " << to_power_of_two(std::numeric_limits<int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<int>::max() << " = " << to_power_of_two(std::numeric_limits<int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<long int>::min() << " = " << to_power_of_two(std::numeric_limits<long int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<long int>::max() << " = " << to_power_of_two(std::numeric_limits<long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned long int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned long int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned long int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** long long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<long long int>::min() << " = " << to_power_of_two(std::numeric_limits<long long int>::min()) << '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<long long int>::max() << " = " << to_power_of_two(std::numeric_limits<long long int>::max()) << '\n'; std::cout << '\n'; std::cout << "**** unsigned long long int ****" << '\n'; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned long long int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned long long int>::min()) */<< '\n'; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned long long int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned long long int>::max()) << '\n'; std::cout << '\n'; std::cout << "*********** Floating Point Ranges ************" << '\n'; std::cout << "**** float ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<float>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<float>::max() << '\n'; std::cout << '\n'; std::cout << "**** double ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<double>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<double>::max() << '\n'; std::cout << '\n'; std::cout << "**** long double ****" << '\n'; std::cout << "Min (±): " << std::dec << std::numeric_limits<long double>::min() << '\n'; std::cout << "Max (±): " << std::dec << std::numeric_limits<long double>::max() << '\n'; return 0; }

Tack!

Permalänk
Medlem

Det händer lite då och då att highlightingen slutar fungera. Men det brukar lösa sig om man startar om vs code eller datorn. Om det är permanent i en fil så kan det vara t.ex. line endings i inklistrade koden som är problemet.

Permalänk
Medlem
Skrivet av swesen:

Det händer lite då och då att highlightingen slutar fungera. Men det brukar lösa sig om man startar om vs code eller datorn. Om det är permanent i en fil så kan det vara t.ex. line endings i inklistrade koden som är problemet.

Testade med "std::endl", samma problem:

#include <limits> #include <cstddef> #include <string> #include <bit> #include <type_traits> #include <sstream> #include <iostream> template <typename T> requires std::is_signed_v <T> std::string to_power_of_two(T val) { const bool negative = val < 0; using U = std::make_unsigned_t <T>; const U uval = negative ? static_cast <U> (- val) : static_cast <U> (val); const auto width = std::bit_width(uval) - negative; std::ostringstream ss; ss << (negative ? '-' : '+') << "2^" << width; if (! negative) { ss << "-1"; } return ss.str (); } template <typename T> requires std::is_unsigned_v <T> std::string to_power_of_two(T val) { std::ostringstream ss; ss << "+2^" << std::bit_width(val); return ss.str (); } int main() { std::cout << "Below is a list of max and min values for all numeric types on this system: " << std::endl; std::cout << "*********** Integer Ranges ************" << std::endl; std::cout << "**** short int ****" << std::endl; std::cout << "Min: " << std::dec << std::numeric_limits<short>::min() /* << " = " << to_power_of_two(std::numeric_limits<short>::min()) */<< std::endl; std::cout << "Max: " << std::dec << std::numeric_limits<short>::max() << " = " << to_power_of_two(std::numeric_limits<short>::max()) << std::endl; std::cout << std::endl; std::cout << "**** unsigned short int ****" << std::endl; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned short int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned short int>::min()) */<< std::endl; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned short int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned short int>::max()) << std::endl; std::cout << std::endl; std::cout << "**** int ****" << std::endl; std::cout << "Min: " << std::dec << std::numeric_limits<int>::min() << " = " << to_power_of_two(std::numeric_limits<int>::min()) << std::endl; std::cout << "Max: " << std::dec << std::numeric_limits<int>::max() << " = " << to_power_of_two(std::numeric_limits<int>::max()) << std::endl; std::cout << std::endl; std::cout << "**** unsigned int ****" << std::endl; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned int>::min()) */<< std::endl; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned int>::max()) << std::endl; std::cout << std::endl; std::cout << "**** long int ****" << std::endl; std::cout << "Min: " << std::dec << std::numeric_limits<long int>::min() << " = " << to_power_of_two(std::numeric_limits<long int>::min()) << std::endl; std::cout << "Max: " << std::dec << std::numeric_limits<long int>::max() << " = " << to_power_of_two(std::numeric_limits<long int>::max()) << std::endl; std::cout << std::endl; std::cout << "**** unsigned long int ****" << std::endl; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned long int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned long int>::min()) */<< std::endl; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned long int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned long int>::max()) << std::endl; std::cout << std::endl; std::cout << "**** long long int ****" << std::endl; std::cout << "Min: " << std::dec << std::numeric_limits<long long int>::min() << " = " << to_power_of_two(std::numeric_limits<long long int>::min()) << std::endl; std::cout << "Max: " << std::dec << std::numeric_limits<long long int>::max() << " = " << to_power_of_two(std::numeric_limits<long long int>::max()) << std::endl; std::cout << std::endl; std::cout << "**** unsigned long long int ****" << std::endl; std::cout << "Min: " << std::dec << std::numeric_limits<unsigned long long int>::min() /* << " = " << to_power_of_two(std::numeric_limits<unsigned long long int>::min()) */<< std::endl; std::cout << "Max: " << std::dec << std::numeric_limits<unsigned long long int>::max() << " = " << to_power_of_two(std::numeric_limits<unsigned long long int>::max()) << std::endl; std::cout << std::endl; std::cout << "*********** Floating Point Ranges ************" << std::endl; std::cout << "**** float ****" << std::endl; std::cout << "Min (±): " << std::dec << std::numeric_limits<float>::min() << std::endl; std::cout << "Max (±): " << std::dec << std::numeric_limits<float>::max() << std::endl; std::cout << std::endl; std::cout << "**** double ****" << std::endl; std::cout << "Min (±): " << std::dec << std::numeric_limits<double>::min() << std::endl; std::cout << "Max (±): " << std::dec << std::numeric_limits<double>::max() << std::endl; std::cout << std::endl; std::cout << "**** long double ****" << std::endl; std::cout << "Min (±): " << std::dec << std::numeric_limits<long double>::min() << std::endl; std::cout << "Max (±): " << std::dec << std::numeric_limits<long double>::max() << std::endl; return 0; }

Permalänk
Hedersmedlem

@Apollo11 Testa att ställa dig sist på några rader, radera tills efterföljande rad hoppar upp och tryck enter för att återställa.

Permalänk
Medlem
Skrivet av Apollo11:

Testade med "std::endl", samma problem:

Menade de osynliga tecken som skrivs ut när man trycker enter.

Testa vad Elgot skrev, tror det går att få vs code att göra alla samma till samma nere i höger hörn.

Permalänk
Medlem
Skrivet av Elgot:

@Apollo11 Testa att ställa dig sist på några rader, radera tills efterföljande rad hoppar upp och tryck enter för att återställa.

Ok, återkommer.

Permalänk
Medlem

Testade vad @Elgot skrev, det fungerade inte.

Permalänk
Hedersmedlem

Jag kan inte reproducera problemet på VSCode på min Windows-PC, men jag har å andra sidan inte heller några särskilda insticksprogram för C++ installerade.

Händer problemet fortfarnade om du copy-pastar koden här från Sweclockers-tråden istället för från din andra fil? Kan ju vara något osynligt skumt tecken som försvunnit på vägen hit till Sweclockers

Permalänk
Medlem
Skrivet av pv2b:

Jag kan inte reproducera problemet på VSCode på min Windows-PC, men jag har å andra sidan inte heller några särskilda insticksprogram för C++ installerade.

Händer problemet fortfarnade om du copy-pastar koden här från Sweclockers-tråden istället för från din andra fil? Kan ju vara något osynligt skumt tecken som försvunnit på vägen hit till Sweclockers

Testade nyss det, fungerade ej. Bild nedan på alla mina plugins för VS Code:

Permalänk
Hedersmedlem
Skrivet av Apollo11:

Testade nyss det, fungerade ej. Bild nedan på alla mina plugins för VS Code:
<Uppladdad bildlänk>

Vad händer om du stänger av alla dina extensions?

Permalänk
Medlem
Skrivet av pv2b:

Vad händer om du stänger av alla dina extensions?

<Uppladdad bildlänk>

Då försvinner all ljusblå färgläggning på cout:

Permalänk
Hedersmedlem
Skrivet av Apollo11:

Då försvinner all ljusblå färgläggning på cout:
<Uppladdad bildlänk>

Då är det ett av dina insticksprogram som orsakar problemet.

Du kan använda Extension Bisect för att ta reda på vilket.
https://code.visualstudio.com/blogs/2021/02/16/extension-bise...