How to ensure attribute/property wrapping within max line length in Visual Studio Code for code formatting

Although my question may seem familiar, I have yet to find a solution after reading through numerous similar queries.

For my project development, I utilize Visual Studio Code with Angular, Typescript, and external libraries. With many lengthy lines of HTML (and more) in the project, I am looking to ensure well-formatted code. Here's a brief example:

Before formatting:

<grid-column field="formatCode" title="Format" width="65" class="status-column cell-with-button" *ngIf="width > 1" [class]="{ codeColumn: true }" [hidden]="columns[7].hidden"></grid-column>

Expected result:

<grid-column field="incoTermCode" title="Incoterm" width="65"
    class="status-column cell-with-button" *ngIf="width > 1"
    [class]="{ codeColumn: true }" [hidden]="columns[7].hidden"
></grid-column>

The desired behavior is to wrap lines when exceeding the max length but without moving each attribute/property to a new line individually. Instead, keeping the line going until reaching the max length, then proceeding to the next line as needed.

Previously, I used Prettier code formatter, but it doesn't offer the exact option I desire as explained here(scroll down for answers). So this is how Prettier formats the above example:

Prettier formatting:

<grid-column
    field="formatCode"
    title="Format"
    width="65"
    class="status-column cell-with-button"
    *ngIf="width > 1"
    [class]="{ codeColumn: true }"
    [hidden]="columns[7].hidden"
></grid-column>

This format is acceptable, but let's consider another example:

Expected:

<div class="valid" *ngIf="form.get('pieces').get([i]).get('quantity').valid>
 <img alt="Validity icon - valid" src="assets/common/images/icons/ValidIcon.svg">
</div>

Prettier output:

<div
  class="valid"
  *ngIf="
    form
      .get('pieces')
      .get([i])
      .get('quantity').valid
  "
>
  <img alt="Validity icon - valid" src="assets/common/images/icons/ValidIcon.svg" />
</div>

Considering these examples are relatively short, imagine the formatting challenges presented by much longer lines in my project. While I appreciate Prettier's efficiency, I aim to avoid bloating my files unnecessarily.

Any suggestions or recommendations would be greatly appreciated!

Answer №1

Turn off prettier and experiment with the following configurations until you achieve the desired formatting:


    "html.format.wrapLineLength": 100,
    "html.format.wrapAttributes": "aligned-multiple"

Answer №2

To fix the formatting, try changing the value of

"html.format.wrapAttributes": "auto"
in your settings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a conditional property in TypeScript based on an existing type - a comprehensive guide

Imagine if I had the following: type Link = { text: string; link: string; } interface BigLink extends Link { some: number; something: string; else: string; } However, there's a variable that shares all these properties except for the fact ...

Error encountered: Dev server does not have an interface defined

I recently utilized the react-ts template to create a project with vite v5. However, when I run the application using pnpm dev, an error message pops up: App.tsx:9 Uncaught ReferenceError: CharacterConnectionStatus is not defined at App.tsx:9:22 Look ...

How can we transform an object into an array using Typescript generics?

Can someone help me with this function? export const ObjectToArray = <T>(object: T): { key: unknown; value: unknown }[] => Object.entries(object).map(o => ({ key: o[0], value: o[1] })); I'm trying to remove the any part from the result ...

Command to update a document in AWS DynamoDB using the Document Client

While attempting to utilize the UpdateCommand feature within the AWS DynamoDB documentation, I encountered various challenges due to its lack of detailed explanation and difficulty in implementation. My aim was to employ the update command to seamlessly t ...

Variety of properties determined by a "type" prop, expanding variations based on a value from the interface

I am trying to enhance a type based on a value from the main interface. If the type == multiline, it will have a specific interface, and if the type == icon, it will have a different type. import React, { memo, useCallback, ReactNode } from 'react&apo ...

What is the best way to dynamically set an ID in Angular using the pound symbol (#)?

I am currently developing a dynamic Angular 6 application that utilizes dynamic components. My approach involves using @ViewChild('<id>', { read: ViewContainerRef }) <id>; to reference the divs where I intend to populate with dynamic ...

Organizing components that display slightly more intricate data: Choosing between storing data, passing props, or combining into a single component

I am looking to develop a component that enables users to edit data for a mathematical classification model. The interface concept may resemble the following: interface MathModel { name: string; article: ArticleDescription; criteria: Criteria; coe ...

Require that the parent FormGroup is marked as invalid unless all nested FormGroups are deemed valid - Implementing a custom

Currently, I am working on an Angular 7 project that involves dynamically generating forms. The structure consists of a parent FormGroup with nested FormGroups of different types. My goal is to have the parentForm marked as invalid until all of the nested ...

HTML not updating after a change in properties

My template is structured as a table where I update a column based on a button click that changes the props. Even though the props are updated, I do not see the template re-rendered. However, since I am also caching values for other rows in translatedMessa ...

Encountering a CORS error specifically when adding an Auth0 token to the request on the API gateway

I'm currently working on hosting a full stack web application in AWS. The setup involves an Angular frontend stored in an S3 bucket behind CloudFront, using a domain from Route53. On the backend, there's a TypeScript Express project behind API Ga ...

Using prevState in setState is not allowed by TypeScript

Currently, I am tackling the complexities of learning TypeScipt and have hit a roadblock where TS is preventing me from progressing further. To give some context, I have defined my interfaces as follows: export interface Test { id: number; date: Date; ...

Is Intellisense within HTML not available in SvelteKit when using TypeScript?

Having trouble with intellisense inside HTML for a simple page component. Also, renaming properties breaks the code instead of updating references. Typescript version: 4.8.4 Below is the code snippet: <script lang="ts"> import type { Bl ...

Converting enums to numbers through JSON serialization

In my C# backend, I have enumerated settings with unique numbers assigned to each. public enum Settings { Setting1 = 1, Setting2 = 2, //... } To send these settings to the client through WebAPI, I use a dictionary structure. public MyModel ...

Verifying the outcomes of a spied function with the callThrough method

Is there a way to validate the outcomes of a spied function in nestjs using jasmine? I have set up a jasmine spy on a method to monitor its arguments and response/exception, but I'm unable to access the return value of the spied method. For example, ...

Utilizing Lazy Loading for the search HomeComponent, which is not being utilized within the module

I'm facing an issue with Lazy Loading in Angular. I have a module called ShopCartModule that needs to be implemented using Lazy Loading. In addition to the ShopCartModule, I also have the AppModule and the CoreModule. This is my AppModule: @NgModul ...

Exclusive Vue3 Props that cannot be used together

How can a component be created that accepts either json with jsonParserRules or jsonUrl with jsonParserRulesUrl, but not both? It would be ideal if the IDE could provide a warning when both props are specified. Example of an Attempt that does not Work < ...

Vitek - Uncaught ReferenceError: Document Is Not Defined

Why am I encountering an error when trying to use File in my vitest code, even though I can typically use it anywhere else? How can I fix this issue? This is the content of my vite.config.ts. /// <reference types="vitest" /> import { defin ...

The data type 'null' is not a valid index type to be used in the Array.reduce() accumulator

This is a follow-up inquiry from: How can JavaScript convert multiple key-value pairs in object lists into one nested object? The initial objective was to merge numerous objects with various key-value pairs into a single nested object. For example, start ...

The 'React' namespace does not contain the exported members 'ConsumerProps' or 'ProviderProps'

Is it possible to install this library in Visual Studio with React version 15.0.35? Are there any other libraries that are compatible with this specific React version? import * as React from 'react'; import { RouteComponentProps, NavLink } from ...

An in-depth guide on integrating lint-staged with jest and utilizing --collectCoverageFrom

I have incorporated lint-staged along with Jest testing framework to solely test the files that have been altered since the last commit, following the instructions outlined in this blog. Here is my current configuration: "src/**/*.{ts}": [ "prettier -- ...